skip to Main Content

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:

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

2

Answers


  1. .arrays[].properties |= map({ type: "FOOBAR", file: "someFile" })
    

    Will result in:

    {
      "arrays": [
        {
          "name": "foo",
          "properties": [
            {
              "type": "FOOBAR",
              "file": "someFile"
            }
          ]
        }
      ]
    }
    

    Try it online!
    Login or Signup to reply.
  2. I would replace the whole properties field :

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

    You can try it here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search