0

I'm writing a shell script to remove the second element in the array using jq where ID= "dimes-snapshot" and time= 2017-01-09T22:41:46.229000+00:00

The array is in a snapshot.json file and I would like the changes to the array be reflected in that file

[
    {
        "ID": "delete-05032019",
        "Time": "2019-05-03T18:01:12.375000+00:00"
    },
    {
        "ID": "dimes-snapshot",
        "Time": "2017-01-09T22:41:46.229000+00:00"
    },
    {
        "ID": "dimes-snapshot-2",
        "Time": "2018-02-28T16:06:09.091000+00:00"
    }
]

I've tried this command but it doesn't work

jq "del(.[] | select(.id == $ID))" snapshots.json 
3
  • Also, "didn't work" isn't enough of an answer to identify a specific problem. If $ID is expanding to something that isn't valid jq syntax, for example, that'll cause a specific, recognizable error message. Include error messages in your question. Commented Mar 22, 2021 at 21:04
  • One more-verbose-than-necessary way to do this, btw, is jq --arg ID "$ID" '[.[] | select(.ID != $ID)]' Commented Mar 22, 2021 at 21:04
  • The jq query shown in the question is correct but the value of the jq variable $ID must be set, e.g. using --arg ID "$ID" on the command line. Commented Mar 22, 2021 at 22:32

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.