Jq: Accessing fields that contain a special character

Created on 1 Jun 2013  路  12Comments  路  Source: stedolan/jq

It seems as there's no way to access a field which contains a special character such as @ or $. For example, let's say I want to extract the value of the @id member from the following document

{
  "@id": "http://example.com/",
  "desc": "cannot extract value of @id member"
}

jq .@context file.json fails with the following error:

error: syntax error, unexpected FORMAT, expecting $end
.@id
 ^^^
1 compile error
support

Most helpful comment

The shell is stripping the quotes. It's good practice to single-quote the whole thing: jq '.["@id"]' test.json

All 12 comments

.["@id"] should do the trick.

No, unfortunately that doesn't work either:

$ jq .["@id"] test.json
jq: error: id is not a valid format

try the array accessor: .["@id"] http://stedolan.github.io/jq/manual/#foo

The shell is stripping the quotes. It's good practice to single-quote the whole thing: jq '.["@id"]' test.json

Unfortunately, that didn't work either (on Windows):

jq '.["@id"]' test.json

error: Invalid character
'.[@id]'
^
error: Invalid character
'.[@id]'
       ^
2 compile errors

What did work was jq ".["""@id"""]" test.json but that's all but intuitive

Ahh, you're running into the horrors of Windows command-line quoting. The correct jq filter is .["@id"]. On Unix systems, you can pass this to jq like so: jq '.["@id"]' (the shell eats the quotes and the stuff between them is passed to jq).

On Windows, you use double quotes, but the rules for escaping are strange. I think doubling the quote should work: jq ".[""@id""]". I think they can also be backslashed: jq ".[\"@id\"]". Good luck if you ever want to pass jq a backslash.

The other alternative is to put your jq filter in a file and use jq -f filename to run the program. That way, you can avoid windows command-line parsing entirely.

Yeah. Well I think we just have to live with that. More descriptive error messages would definitely help though. I don't know the internals of jq at all, but wouldn't the simplest thing be to expand an error message like

error: syntax error, unexpected FORMAT, expecting $end

to

error: Invalid filter. The "." selector cannot be followed by a format string. Expected: $end

Probably other rules should be evaluated as well as it can also be followed by a token (string) or an array identifier ([]).

Closing this after 2c4ccd1, see #141 for more.

@I am having a similar issue. my json looks like this
{ "_t": "JSONObject", "192-168-100-100": { "ipAddress": "192.168.100.100", "cpuLoad": 30, "_t": "JSONObject", "callersOnHold": 1 } }
And I need to access callersOnHold
But I cant get to/past the "192-168-100-100"
`

jq ".192-168-100-100" status

-367.808
`
This is on ubuntu.. Thanks for any help in advance!

@joshzitting jq '.["192-168-100-100"]'

@slapresta oh my heck! thank you! I swear I have tried every variation!

No problem! For other usage questions, please head over to StackOverflow's jq tag

Was this page helpful?
0 / 5 - 0 ratings