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

Bash jq, how to read a json file where field names are like numbers?

I can’t access the values ​​in the json file.
Can be a bug for labels like numerics? Or I wrong to use jq? Can you help me?
Below json sample
Sorry for my English, txs

{
    "datasets":{
        "0":
            {
                "0": { "0":11, "1":11,"2":10 },
                "1": { "0":73, "1":77, "2":87 },
                ....,
                ....,
                "n":{ "0":1027, "1":1025, "2":1020 }
            },
        "1":
            {
                "0": { "0":8, "1":7, "2":12 },
                "1": { "0":69, "1":75, "2":77 },
                ....,
                ....,
                "n":{ "0":1026, "1":1026, "2":1025 }
            },
        ....,
        ....,

        "99": 
            {
                "0": { "0":9, "1":9, "2":10 },
                "1": { "0":77, "1":76, "2":75 },
                ....,
                ....,
                "n":{ "0":1010, "1":1011, "2":1011 }
            },
    },
    "other_label":{
        .....
    }
}

I used jq command following manual but unsuccessfully. If I run:
jq .datasets
command is executed without problem. But if I add ."0" or .0 then results in:
jq: error: syntax error, unexpected LITERAL, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:

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

>Solution :

Like the error message says, it’s a shell quoting issue. Given this input file:

{
    "datasets":{
        "0":
            {
                "0": { "0":11, "1":11,"2":10 },
                "1": { "0":73, "1":77, "2":87 },
                "n":{ "0":1027, "1":1025, "2":1020 }
            },
        "1":
            {
                "0": { "0":8, "1":7, "2":12 },
                "1": { "0":69, "1":75, "2":77 },
                "n":{ "0":1026, "1":1026, "2":1025 }
            },
        "99": 
            {
                "0": { "0":9, "1":9, "2":10 },
                "1": { "0":77, "1":76, "2":75 },
                "n":{ "0":1010, "1":1011, "2":1011 }
            }
    },
    "other_label":{
    }
}

I can run this command line:

jq '.datasets."0"' data.json

And get this output:

{
  "0": {
    "0": 11,
    "1": 11,
    "2": 10
  },
  "1": {
    "0": 73,
    "1": 77,
    "2": 87
  },
  "n": {
    "0": 1027,
    "1": 1025,
    "2": 1020
  }
}

You need the outer quotes so that the quotes around "0" are actually seen by jq.

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