Jq: Comments

Created on 28 Apr 2017  路  3Comments  路  Source: stedolan/jq

While I am fully aware that the JSON specification does not provide any mechanism for comments, it is a fact that several JSON parsers are sufficiently lax to permit them (in as far as they ignore them), Chrome being a notable example.

This request is only to extend this grace to the jq json parser. By no means should jq support outputting comments or parsing comments into data structures, but C-style comments _are_ often used in human-readable JSON files and knowing that the jq parser can parse and strip these illegal sequences would be a plus in many use cases such as prototyping, etc.

Effectively, this is a feature-request for the following behaviour:

$ jq -c . <<< '
> {
>     "a": "b" // a is b
> }'
{"a":"b"}
dup

Most helpful comment

I think it'd be nice to have either an external program, or an internal parser, that can parse all sorts of broken but intelligible JSON texts (though it would need many options, which is why I'd want this as an external program) and fix it.

For example:

  • tolerate trailing commas
  • make commas optional where possible
  • on fatal parser errors attempt to find the next top-level text (this is very tricky though)
  • ignore errors where possible
  • allow chunking of strings ("first part"", second part")
  • tolerate embedded unescaped ASCII control chars (oof, definitely this should not be on by default)
  • default missing object values to null ({"a":1,"b","c"})
  • allow ident-like object keys to not have to be quoted ({a:1})
  • the perennial handle-comments thing
  • numbers with thousands separators
  • tolerate incorrect closings ({"a":[1,2})
  • single-quoted strings
  • and who knows what other silly brokenness

Also, convert to/from various binary JSON forms.

But I really think this belongs in an external program, except perhaps for semantically-compatible alternative encoding standards (binary JSON forms).

All 3 comments

```{.sh}
$ cat >> ~/.jq <<<'def decomment:[inputs|gsub("^(?(\"[^\"]\"|[^/\"]|(?)(//.*)?$";"(.j)")]|join("\n")|fromjson;'
$ jq -Rn decomment <<< '

{
"a": "b" // a is b
}'
{"a":"b"}
```

See #402, #695, and others.

I think it'd be nice to have either an external program, or an internal parser, that can parse all sorts of broken but intelligible JSON texts (though it would need many options, which is why I'd want this as an external program) and fix it.

For example:

  • tolerate trailing commas
  • make commas optional where possible
  • on fatal parser errors attempt to find the next top-level text (this is very tricky though)
  • ignore errors where possible
  • allow chunking of strings ("first part"", second part")
  • tolerate embedded unescaped ASCII control chars (oof, definitely this should not be on by default)
  • default missing object values to null ({"a":1,"b","c"})
  • allow ident-like object keys to not have to be quoted ({a:1})
  • the perennial handle-comments thing
  • numbers with thousands separators
  • tolerate incorrect closings ({"a":[1,2})
  • single-quoted strings
  • and who knows what other silly brokenness

Also, convert to/from various binary JSON forms.

But I really think this belongs in an external program, except perhaps for semantically-compatible alternative encoding standards (binary JSON forms).

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tbelaire picture tbelaire  路  4Comments

geoffeg picture geoffeg  路  3Comments

mcandre picture mcandre  路  3Comments

thelonious picture thelonious  路  4Comments

jfeist picture jfeist  路  4Comments