Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQ issues with comments on Json file

Tags:

bash

shell

jq

I'm using JQ https://stedolan.github.io/jq/ to work in bash with my json and when I read the json is throwing me an error

   parse error: Invalid numeric literal at line 2, column 5=

Since my json has some comments

  // comment
  "spawn": {}

I've been seen looking the options and I cannot find any option to fix the problem. Any idea how to solve it?

like image 675
paul Avatar asked Oct 26 '15 18:10

paul


4 Answers

JSON and thus jq do not support comments (in the usual sense) in JSON input. The jq FAQ lists a number of tools that can be used to remove comments, including jsonlint, json5, and any-json. I'd recommend one that can act as a filter.

See https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json for links and further details.

like image 182
peak Avatar answered Oct 08 '22 10:10

peak


Remove them; JSON does not support comments.

(JSON is defined here; you can see a briefer description of the grammar here.)

like image 36
chepner Avatar answered Oct 08 '22 10:10

chepner


I found https://github.com/sindresorhus/strip-json-comments-cli which allows you to do:

cat my_json_with_comments.json | strip-json-comments | jq  .
like image 45
cbenz Avatar answered Oct 08 '22 10:10

cbenz


Can be stripped out using sed, eg to remove lines beginning with '//':

cat test.json | sed 's/^ *\/\/.*//' | jq <>commands>

sed is a pass-through/stream editor, in this case it's substituting nothing ( // ) for lines that begin with '//'; '//' must be escaped with a backslash character since the '/' is used by sed as a delimiter.

like image 2
MikeW Avatar answered Oct 08 '22 10:10

MikeW