55

I'm trying to get the following to work and it's not, help me please:

curl -s 'https://cryptofresh.com/api/asset/markets?asset=MKR' | jq .OPEN.BTC

The variable in question includes a period, I tried just about everything to escape the period && also tried surrounding it in quotes; no go ; this is the variable I'm looking to pull from (I ran jq without any filters, and truncated the output here to show what I need. Thanks in advance future problem solver!

curl -s 'https://cryptofresh.com/api/asset/markets?asset=MKR' | jq
....
 "OPEN.BTC": {
"volume24": 0.932166,
"price": 0.09995,
"updated": "2016-05-04T03:03:29.000Z"
},
....
2240
  • 1,547
  • 2
  • 12
  • 30
Jonny Gerold
  • 551
  • 1
  • 4
  • 3

3 Answers3

95

When a key contains characters that are invalid for identifiers, you'll have to quote the name.

."OPEN.BTC"

Or for older versions of jq, use an index.

.["OPEN.BTC"]

Example

... | jq '."OPEN.BTC"'
Abel Callejo
  • 13,779
  • 10
  • 69
  • 84
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • 5
    I tried both of those, they both return null for me, I'm on jq 1.5 – Jonny Gerold May 04 '16 at 04:58
  • 36
    Make sure you quote the filter, preferably with single quotes. Don't leave it unquoted. i.e., do this: `... | jq '."OPEN.BTC"'` – Jeff Mercado May 04 '16 at 05:02
  • I can't say for jq, but you usually call the later bracket notation, and: `a.b.c` dot notation. – Andreas Louv Jun 13 '16 at 23:15
  • 1
    I suggest to put the comment `jq '."OPEN.BTC"'` as the reply, because it does not work without the single quotes (I'm using jq 1.6). – Elad Tabak Jan 12 '21 at 07:46
  • 1
    Another option is to escape the quotes. jq .\"OPEN.BTC\" – Christian Schneider May 12 '21 at 07:36
  • 1
    @JeffMercado can you plz include your suggestion of using single quote in the answer itself. It might save some time for the next person – Harry Jun 24 '21 at 13:11
  • 1
    For these poor souls using win powershell terminal (default in vscode and win terminal), be sure to escape the double quotes even when inside single quotes, e.g. jq '.[] | .tags.\"http.url\"' sample.json. I've posted an edit to this answer for better readability. – tstojecki May 25 '23 at 05:00
24

Another answer didn't work for me but the comment written by @jeff-mercado worked for me. So, adding it as an answer here.

If your key has dots like "OPEN.BTC" then your jq command should be

curl -s 'https://cryptofresh.com/api/asset/markets?asset=MKR' | jq '."OPEN.BTC"'

Put the key first in double quotes and then escape the first dot by wrapping it using single quotes.

Kumaran
  • 3,460
  • 5
  • 32
  • 34
0

None of these work for me as the field in question is called "end". The only way to do it was to replace the word "end" with something else e.g. EndIt using sed - and then use EndIt as a field for jq to parse to.

user2982122
  • 161
  • 1
  • 5