-1

Need help to parse and print only category values either using jq or jsawk or shell script.

{
  "fine_grained": {
    "dog": [
      {
        "category": "cocker spaniel",
        "mark": 0.9958831668
      }
    ]
  },
  "coarse": [
    {
      "category": "dog",
      "mark": 0.948208034
    }
  ]
}
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
Joseph
  • 17
  • 2
  • 1
    Parse and print 'category' values from *what*? – Reti43 Dec 23 '15 at 00:10
  • Sorry, but we have no idea what you're asking for. Please provide a sample input, its expected output and your best attempt at solving the problem so far. –  Dec 23 '15 at 00:16

2 Answers2

1

Assuming all category values are simple strings and you want all category values, regardless of where it is in the JSON, you could use this filter using jq:

.. | objects.category // empty

This returns the following strings:

"cocker spaniel"
"dog"
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
0

Here is a solution which uses leaf_paths and select to find all the paths with a leaf "category" member and then extract the corresponding values with foreach

foreach (leaf_paths | select(.[-1] == "category")) as $p (
    .
  ; .
  ; getpath($p)
)

If your input is in a file called input.json and the above filter is in a file called filter.jq then the shell command

jq -f filter.jq input.json

should produce

"cocker spaniel"
"dog"

You can use the -r flag if you don't want the quotes in the output.

EDIT: I now realize a filter of the form foreach E as $X (.; .; R) can almost always be rewritten as E as $X | R so the above is really just

  (leaf_paths | select(.[-1] == "category")) as $p
| getpath($p)
jq170727
  • 13,159
  • 3
  • 46
  • 56