Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Editing json file in line with jq

I have a json file like this:

{
"arrays": [
    {
      "name": "foo",
      "properties": [
        {
          "type": "app",
          "url": "https://example.com",
          "checksum": "d6fd580fe890b826250aa5d5a88bcdb28839cff4d447ba51536cda4ceae89c4e"
        }
      ]
    }
  ]
}

I want to change the type to something else, delete the url and checksum key and add a file key in their place with a value.

The final thing should be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
"arrays": [
    {
      "name": "foo",
      "properties": [
        {
          "type": "Bar",
          "file": "filename"
        }
      ]
    }
  ]
}

How would I do this inline with jq

I got upto modifying the type value:

jq '(.arrays[]| select(.name == "foo").properties[]).type |= "Bar"' test.json

>Solution :

I would replace the whole properties field :

jq '(.arrays[]| select(.name == "foo")).properties |= [{
    "type" : "bar",
    "file" : "filename"
}]' test.json

You can try it here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading