1

I have a REST API, which returns something like this:

{
  "foo": 1,
  "bar": 2,
  "foo-bar": 3
}

when I do `http /endpoint/url | jq '.foo-bar', it gave the following error:

jq: error (at <stdin>:1): null (null) and boolean (true) cannot be subtracted

it looks like jq thinks I'm trying to do arithmetic operation with foo-bar.

How do I correctly form this kind of path? Or this is a bug of jq?

peak
  • 105,803
  • 17
  • 152
  • 177
lang2
  • 11,433
  • 18
  • 83
  • 133

1 Answers1

1

In JSON text, JSON keys are always double-quoted. Perhaps your REST API was formatting it properly in double-quotes and your example in your last edit was incorrect. Because without the same jq cannot parse the syntax as a valid JSON.

As far the issue you are seeing, you need to put the field within quotes to let jq know that it is a single field foo-bar you are accessing and not as separate fields

jq '."foo-bar"'

Or more specifically use the array access operator as jq '.["foo-bar"]'

peak
  • 105,803
  • 17
  • 152
  • 177
Inian
  • 80,270
  • 14
  • 142
  • 161
  • 1
    `.foo-bar` is parsed as `.foo - bar` so if there is no function named bar, an error condition will be raised with the message " bar/0 is not defined ..." – peak Jan 23 '18 at 13:17