User Tools

Site Tools


wiki:use_jq_utility_pretty_format_json

Use jq utility to pretty format json

If you get an error like parse error: Invalid numeric literal at line 1, column 7

this is could be because the json contains single quotes instead of double quotes. This is invalid JSON:

{'red': {'hue': {'dark': 2, 'white': 1}}, 'blue': {'blue': {'dark': 3, 'white': 3}, 'blue-2': {'dark': 3, 'white': 3}}}

JSON uses double quotes for strings. Use this to pipe the command to sed. Notice the double quotes around the whole json in echo command (it won't work otherwise):

echo "{'red': {'hue': {'dark': 2, 'white': 1}}, 'blue': {'blue': {'dark': 3, 'white': 3}, 'blue-2': {'dark': 3, 'white': 3}}}" | sed 's/'\''/"/g' | jq .

Output:

{
  "red": {
    "hue": {
      "dark": 2,
      "white": 1
    }
  },
  "blue": {
    "blue": {
      "dark": 3,
      "white": 3
    },
    "blue-2": {
      "dark": 3,
      "white": 3
    }
  }
}

Another problem could be unquoted true or false statements, so quote those instances in the file.

“freeshipping”: false → “freeshipping”: “false”

Tested on

  • Ubuntu 20.04.4 LTS

See also

References

wiki/use_jq_utility_pretty_format_json.txt · Last modified: 2023/07/20 11:38 by antisa

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki