jqlang / jq

Command-line JSON processor
https://jqlang.github.io/jq/
Other
30.41k stars 1.57k forks source link

Allow for looser Javascript style objects to be parsed #1560

Open coreyog opened 6 years ago

coreyog commented 6 years ago

I'm not always in control of where I get my json such as parsing the output of npm version:

{ npm: '5.6.0',
  ares: '1.13.0',
  cldr: '32.0',
  http_parser: '2.7.0',
  icu: '60.1',
  modules: '59',
  nghttp2: '1.25.0',
  node: '9.3.0',
  openssl: '1.0.2n',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.18.0',
  v8: '6.2.414.46-node.15',
  zlib: '1.2.11' }

npm version is weird with pipes but if you redirect it to a file and pass the file through jq I get the error: parse error: Invalid literal at line 1, column 6.

jq -h does not list a flag that would help parse this. Maybe a -js flag or simply -j with the function of looser, javascript-style parsing of the input such as optionally unquoted keys and the allowance of single quotes instead of double quotes for strings.

pkoppstein commented 6 years ago

The maintainers of jq are wary of additional command-line flags in general, and there is much to be said for the "*ix" pipeline philosophy.

Philosophy aside, there is quite a lot about handling quasi-JSON on the jq FAQ (see especially the section https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json)

In this particular case, hjson does what's needed:

$ hjson -j input.js
{
  "npm": "5.6.0",
  "ares": "1.13.0",
  "cldr": "32.0",
  "http_parser": "2.7.0",
  "icu": "60.1",
  "modules": "59",
  "nghttp2": "1.25.0",
  "node": "9.3.0",
  "openssl": "1.0.2n",
  "tz": "2017c",
  "unicode": "10.0",
  "uv": "1.18.0",
  "v8": "6.2.414.46-node.15",
  "zlib": "1.2.11"
}

With a bit more typing, any-json also does the job:

$ any-json --input-format json5 --output-format json  input.js