jsonjqto-jsonfromjson

How do I update values in a json string using jq?


I have a JSON file with content this:-

{
  "content": "{\"accountNumber\":\"12345\",\"transactionId\":\"568\",\"socialSecurityNumber\":\"123456796\",\"identificationNumber\":1,\"securityCode\":\"1234\",\"dateOfBirth\":\"1000-01-01\",\"firstName\":\"qwerty\",\"lastName\":\"xyz\",\"balance\":123}",
  "contentType": "application/json",
  "createdAt": "2020-11-11T12:55:41.350+0000",
  "cryptoKeyId": null
}

I just want to update the value of firstName. End result I want is

{
  "content": "{\"accountNumber\":\"12345\",\"transactionId\":\"568\",\"socialSecurityNumber\":\"123456796\",\"identificationNumber\":1,\"securityCode\":\"1234\",\"dateOfBirth\":\"1000-01-01\",\"firstName\":\"abcdef\",\"lastName\":\"xyz\",\"balance\":123}",
  "contentType": "application/json",
  "createdAt": "2020-11-11T12:55:41.350+0000",
  "cryptoKeyId": null
}

I tried this

 ( .content | fromjson.firstName = "abcdef"  )

but no success. Can you help me here? Thanks


Solution

  • Since you want to update the value, you would use |=, and don’t forget the call to tojson:

    .content |= (fromjson | (.firstName = "abcdef") | tojson)