Ckym Ckym - 4 months ago 43
JSON Question

Find and replace json in a file using jq

I am new at using jq and could use help with the following...

In essence, I am trying to "find and replace" a block of json in a file using a bash script.

I have a file called docs.json to where I retrieved a set of doc entries:

{
"number_of_docs": "1000",
"docs": [{
{
"_id": "000001",
"_rev": "0",
"_content": "abcdefg"
},
{
"_id": "000002",
"_rev": "0",
"_content": "hijklmn",
"_attachments": {
"image1.png": {
"content_type": "image/png"
...
}
}
},
{
"_id": "000003",
"_rev": "0",
"_content": "opqrstuv"
}]
}


And I'm trying to replace all the doc entries (the entire block, not just the value of the keys) that have "_attachments" with a new block of json that is stored in a variable named "doc":

{
"_id": "000002_masked",
"_rev": "0",
"_content": "[Document content has been masked for confidentiality.]",
"_attachments": {
"confidential.png": {
"content_type": "image/png"
...
}
}
}


Is what I'm trying to do possible with jq and running from within a bash script?

Thank you for the help and tips, in advance.

Answer

The key is to select the documents that have the _attachments key, then replace them with the |= operator.

jq --argjson new "$doc" '(.docs[]| select(has("_attachments"))) |= $new' tmp.json

--argjson is used to pass the value of $doc into the filter as a raw JSON value.

Comments