3

I have a (not so complicated) json file and I need to extract its contents using bash. I want to use jq for the processing, it should be straightforward. The problem is that I'm getting a weird error in the processing that I don't know how to solve (because I don't know what is causing it).

A minimal sample causing me problems:

{
  "E23763": {
    "data": "information"
  }
}

If I just run jq to pretty-print it, it works:

$ cat test.json | jq .
{
  "E23763": {
    "data": "information"
  }
}

But if I try to extract the first field, it fails criptically:

$ cat test.json | jq .E23763
jq: error: Invalid numeric literal at EOF at line 1, column 7 (while parsing '.E23763') at <top-level>, line 1:
.E23763
jq: 1 compile error

The expected result would had been:

{
  "data": "information"
}

Anyone found a similar issue? Why it is complaining about a numeric literal when he is really looking into a string?

Quotation didn't seem to matter here, same error.

Poshi
  • 5,332
  • 3
  • 15
  • 32

1 Answers1

2

Please refer to this issue on GitHub there are many responses posted here which might help you with your problem: https://github.com/stedolan/jq/issues/1526

I'll post one of the solutions here however:

jq '.["E23763"]' test.json

Another Solution as said by @Inian is:

jq '."E23763"' json

Without the [], in this case it was the correct solution but try both nonetheless

Basically the parser is buggy and treats .E as the beginning of a number.

peak
  • 105,803
  • 17
  • 152
  • 177
Andre Motta
  • 759
  • 3
  • 16
  • Thanks, I will check the github issue ASAP. BTW, I'm using the latest version at this moment (1.5, checked 10 minutes ago), and the workaround you proposed is returning me "null" :-( – Poshi Jul 13 '18 at 12:56
  • @Poshi: See my edit to the answer. It was wrong previously – Inian Jul 13 '18 at 12:57
  • try using my answer or @Inian with a -c. I'm not in a linux environment right now (work pc so i can't install what i want). So i can't test it but try: jq -c '.["E23763"]' test.json or jq -c '."E23763"' json. If it doesn't work try with raw -r – Andre Motta Jul 13 '18 at 12:59
  • It works when you strip the `[]` chars. Thanks! – Poshi Jul 13 '18 at 13:06
  • @Inian i will edit my answer and tag you so that future people that read this might find it easier! – Andre Motta Jul 13 '18 at 13:09