如何使用 jq 更新 json 字符串中的值?

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

我有一个 JSON 文件,内容如下:-

{
  "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
}

我只想更新 firstName 的值。 我想要的最终结果是

{
  "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
}

我试过了

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

但没有成功。 你能帮我吗? 谢谢

因为你想更新值,你会使用 |=,并且不要忘记调用 tojson:

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