0

I have a JSON file with thousands of JSON objects, such as below. I want to compare the values of st1 against st2.

For every object in the JSON file: If st1 is larger than st2, copy that object into another file.

{
    "in": 4,
    "li": [{
        "fa": 28806,
        "fs": 00002,
        "fb": 21894
    }],
    "ome": {
         "ms": "00000000000"
    },
    "st2": 1508584163,
    "ei": {
        "ev": 0
    },
    "rn": 4,
    "st1": 1508584147
}

I have tried using jq, or is it easier doing it with awk even though it's a JSON file?

1
  • 2
    what exactly should be copied? elaborate your question Commented Nov 8, 2017 at 18:13

1 Answer 1

4

Definitely use jq to parse JSON data:

$ cat file.json
{ "st1":0, "st2":10 }
{ "st1":0, "st2":0 }
{ "st1":10, "st2":0 }

$ jq -c 'select(.st1 > .st2)' file.json
{"st1":10,"st2":0}

This can be redirected into another file.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .