Parse or view JSON data fields using JQ tool utility where field names have a "-" dash in the key name

Related searches

I have a JSON data file (as shown below) and I'm trying to find field values using jq utility.

It's working fine except for fields if the key name contains a - dash character in it.

How can I get the values of "field-2", "field-three" or "field-three.url" for element under content.book1 (using jq at least)?

I tried the following to get the values but it's giving me the following errors for fields whose key name contains a dash - in it's name. I tried to back slash - character but that didn't help either.

Error types found:
jq: error (at <stdin>:27): null (null) and number (2) cannot be subtracted
jq: 1 compile error

jq: error: three/0 is not defined at <top-level>

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>

jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:

Commands:

$ cat /tmp/my.data.json
{
  "pages": {
    "book1": [
      "page1",
      "page2-para1",
      "page3-para1-sentence1",
      "page3-para2-sentence3-word4"
    ]
  },
  "content": {
    "book1": {
      "name": "giga",
      "url": "-",
      "field1": "value1",
      "field-2": "value-2",
      "field-three": {
        "name": "THIRD",
        "url": "book1/field-three/",
        "short-url": "book1/field-three/chota-chetan"
      },
      "authur": {
        "name": "lori CHUCK",
        "displayIndex": 4
      },
      "route": "/in-gc/hindi-chini-bhai-bhai"
    }
  }
}

$ cat /tmp/my.data.json| jq ".pages"
{
  "book1": [
    "page1",
    "page2-para1",
    "page3-para1-sentence1",
    "page3-para2-sentence3-word4"
  ]
}

$ cat /tmp/my.data.json| jq ".pages.book1[0]"
"page1"

$ cat /tmp/my.data.json| jq ".pages.book1[1]"
"page2-para1"

$ cat /tmp/my.data.json| jq ".content"
{
  "book1": {
    "name": "giga",
    "url": "-",
    "field1": "value1",
    "field-2": "value-2",
    "field-three": {
      "name": "THIRD",
      "url": "book1/field-three/"
    },
    "authur": {
      "name": "lori CHUCK",
      "displayIndex": 4
    },
    "route": "/in-gc/hindi-chini-bhai-bhai"
  }
}

$ cat /tmp/my.data.json| jq ".content.book1"
{
  "name": "giga",
  "url": "-",
  "field1": "value1",
  "field-2": "value-2",
  "field-three": {
    "name": "THIRD",
    "url": "book1/field-three/"
  },
  "authur": {
    "name": "lori CHUCK",
    "displayIndex": 4
  },
  "route": "/in-gc/hindi-chini-bhai-bhai"
}

$ cat /tmp/my.data.json| jq ".content.book1.name"
"giga"

$ cat /tmp/my.data.json| jq ".content.book1.field1"
"value1"

$ cat /tmp/my.data.json| jq ".content.book1.field-2"
jq: error (at <stdin>:27): null (null) and number (2) cannot be subtracted

$ cat /tmp/my.data.json| jq ".content.book1.field-three"
jq: error: three/0 is not defined at <top-level>, line 1:
.content.book1.field-three
jq: 1 compile error

$ cat /tmp/my.data.json| jq ".content.book1.field-three.url"
jq: error: three/0 is not defined at <top-level>, line 1:
.content.book1.field-three.url
jq: 1 compile error

$ cat /tmp/my.data.json| jq ".content.book1.field\-2"       
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.content.book1.field\-2                    
jq: 1 compile error

$ cat /tmp/my.data.json| jq ".content.book1.field\\-2"
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting $end (Unix shell quoting issues?) at <top-level>, line 1:
.content.book1.field\-2                    
jq: 1 compile error

$ cat /tmp/my.data.json| jq ".content.book1.'field-2'"
jq: error: syntax error, unexpected INVALID_CHARACTER, expecting FORMAT or QQSTRING_START (Unix shell quoting issues?) at <top-level>, line 1:
.content.book1.'field-2'               
jq: 1 compile error

$ cat /tmp/my.data.json| jq ".content.book1.authur"
{
  "name": "lori CHUCK",
  "displayIndex": 4
}

$ cat /tmp/my.data.json| jq ".content.book1.route"
"/in-gc/hindi-chini-bhai-bhai"

$

PS: I already know egrep so that's not what I'm looking for.

cat /tmp/my.data.json| jq ".content.book1"|egrep "short-url|field-2"
  "field-2": "value-2",
    "short-url": "book1/field-three/chota-chetan"

and someone really did a great job here: https://jqplay.org/

"-" is used for negation in jq. For key names with special characters such as "-", one cannot use the simplified ".keyname" syntax. There are several alternatives, but the most robust is simply to use the form .["KEY NAME"], which can be abbreviated to ["KEY NAME"] when chained, e.g. .a["b-c"] is shorthand for .a | .["b-c"].

If in doubt, use the pipe explicitly.

For further information, please consult the jq manual and/or https://github.com/stedolan/jq/wiki/FAQ

Parsing JSON with jq, Most of the popular API and data services use the JSON data format, If you've haven't installed the pup HTML parsing tool, or any other tool Below, the things_carried attribute has an Array containing apples , hat , and harmonica . user@host:~$ cat ernst.json | jq '.name.first' "Joni" user@host:~$ cat� JSON and jq. JSON stands for JavaScript Object Notation. It’s a scheme that allows data to be encoded into plain text files, in a self-describing way. There are no comments in a JSON file—the contents should be self-explanatory. Each data value has a text string called a “name” or “key.” This tells you what the data value is.

I don't know about jq, but you put python in the tags so:

$ cat test.json | python -c "import sys, json; print(json.load(sys.stdin)['content']['book1']['field-three']['name'])"
THIRD

or without the pipe:

$ python -c "import json; print(json.load(open('test.json'))['content']['book1']['field-three']['name'])"

Reshaping JSON with jq, Working with data from an art museum API and from the Twitter API, this to use the command-line utility jq to filter and parse complex JSON files However, many tools for data analysis and visualization require input in In brief, a JSON object is a series of key/value pairs, where keys are the names for� Parse or view JSON data fields using JQ tool utility where field names have a “-” dash in the key name (4 answers) Closed 2 years ago . I use jq 1.5 in a Windows Environment to modify an json object that i receiving from Amazon s3.

As explained in the jq manual, to handle keys with non-identifier characters like - you can use double quotes.

From the shell this is easiest if you use single quotes around your filter. For example, try the following commands:

cat /tmp/my.data.json | jq '.pages'
cat /tmp/my.data.json | jq '.pages.book1[0]'
cat /tmp/my.data.json | jq '.pages.book1[1]'
cat /tmp/my.data.json | jq '.content'
cat /tmp/my.data.json | jq '.content.book1'
cat /tmp/my.data.json | jq '.content.book1.name'
cat /tmp/my.data.json | jq '.content.book1.field1'
cat /tmp/my.data.json | jq '.content.book1."field-2"'
cat /tmp/my.data.json | jq '.content.book1."field-three"'
cat /tmp/my.data.json | jq '.content.book1."field-three".url'
cat /tmp/my.data.json | jq '.content.book1.authur'
cat /tmp/my.data.json | jq '.content.book1.route'

JSON on the command line with jq, A series of how to examples on using jq, a command-line JSON processor. Suppose that we have a file names.json containing the following json. [{"id": 1, " name": Because jq is UNIX friendly it is possible to pipe data in and out of it. This can be useful for using jq as a filter or interacting with other tools. Using jq. jq is a program described as “sed for JSON data":. You can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you

Hm..took some time but finally it seems like we need to DOUBLE QUOTE it and back slash just the double quote for any key name containing a - in it's name.

$ cat /tmp/my.data.json| jq ".content.book1.\"field-2\""                    
"value-2"

$ cat /tmp/my.data.json| jq ".content.book1.\"field-three\".\"url\""  
"book1/field-three/"

OR if you wrap everything in a single quotes ', then we do NOT need to backslash " double quotes but use double quotes for key names with - in their name.

$ cat /tmp/my.data.json| jq '.content.book1."field-three"."url"'  
"book1/field-three/"

Hope it helps! Took some help/hint from https://jqplay.org/

See this for more: https://github.com/stedolan/jq/issues/38#issuecomment-9770240

JQ doesn't support dash `-` character in dictionary key names , GitHub has a JSON API, so let's play with that. This URL gets us the last 5 commits from the jq repo. jq '.[0] | {message: .commit.message, name: .commit. committer.name}' which builds an object out of those fields). Here we're making an object as before, but this time the parents field is being set to [.parents [].html_url]� JSON is a lightweight format that is nearly ubiquitous for data-exchange. jq is a command-line tool for parsing JSON. Most of the popular API and data services use the JSON data format, so we'll learn how it's used to serialize interesting information, and how to use the jq to parse it at the command-line.

jq Tutorial, json - same - jq select multiple fields from array Using jq to parse and display multiple fields in a json serially (4) I have this Json

Include in Output: The selected column is included in the data stream coming out of the tool. Choose how the JSON gets parsed. Choices include: Output values to a single field: Two fields are output, JSON Name and JSON_ValueString. Looking at the input example above, the following data comes out of the tool:

Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

Comments
  • Opened an issue if it's valid: github.com/stedolan/jq/issues/1464
  • This question has been asked before, e.g. at stackoverflow.com/questions/37344329
  • Possible duplicate of jq not working on tag name with dashes
  • Agree, In my post the only extra difference that I can see is that, I have listed the type of error messages to catch the post. Thanks peak.
  • Nice, I'll accept this as an answer but thanks to every one.
  • Thanks Alex; with Python, I already used the same solution like you said but wondering what's bad in jq :) and why it's giving me an error for such a simple operation