jq
jq copied to clipboard
json5 / hjson support?
Do you consider supporting json5 / hjson? Or is there any fast program that convert them to plain json?
@ololoe - Please see the jq FAQ about hjson and similar utilities.
As best I can tell, there is no momentum towards supporting json5 or hjson, but there is perhaps some hope that jq will be able to ignore the type of comments that you see in "raw" gists such as
https://gist.githubusercontent.com/rominirani/8235702/raw/a50f7c449c41b6dc8eb87d8d393eeff62121b392/employees.json
json5 on your link is using nodejs, I think it will be slower or at least start slower than C(++) program. Is there a reason why jq does not extend its parser for at least comments?
See https://github.com/stedolan/jq/issues/1571 regarding jq's support for comments. (The historically minded might also be interested in https://github.com/stedolan/jq/issues/402 .)
It seems to me that there have been three main stumbling blocks in the way of jq allowing comments:
a) There are many ways of commenting JSON, and there seems to be no way to make everyone happy;
b) jq aims to be very lightweight;
c) There have been (and arguably continue to be) higher-priority items for jq.
In my opinion, though, the time is ripe for a minimalist enhancement that would permit jq to ignore the '//' comments that often appear as the first few lines of a file. This convention is in widespread use (e.g. gist; datasette) and it would be very helpful if jq understood it; it would not require much effort to implement; and it would not preclude future enhancements in the same direction.
a) Use ECMA-compatible comments. b) Comment skipping is a very minor change to the parser
I would accept if there would be a converter from json5 / hjson that would be as fast as jq.
Surely it would be better to use jq itself to filter out the comments etc, e.g. to condition a rawfile input into pure JSON.
That way all the complexity of converting from non-pure-JSON formats to pure JSON could be kept out of the jq core and moved out to input filter library scripts that are simply jq code.
e.g. --filterfile mydata mydata.json json5.jq
Here json5.jq would be an external library script written in jq that would read in data as a --rawfile and output it as pure JSON.
Hey folks, If I had a PR to add json5 support, would the jq maintainers take it, at least in principle?
- Initially json5 support would be such that comments are ignored & stripped.
- As a follow-up we can explore preserving comments such as by associating them with the next token, as has been suggested in #1571.
- Additionally it would be alright at least initially to output json even if the input was json5, since it's trivial to convert json output to json5 using other tools. Though I agree this wouldn't add a lot of value on its own, since jq can almost parse json5 as a jq program (suggested in #402). Later we can offer a switch to output json5 if that's acceptable.
I wanted to check first with jq owners if it's ok to explore json5 support inside jq. I don't know how important it is to you that jq only handles strict json format, vs the opportunity of supporting json5 files (or round-tripping them through json for processing and back for output).
@shaybarak was this PR ever added?
The last jq release was nearly 4 years ago, so I'm not holding my breath on this feature being addressed anytime soon.
I'll note that we've been using json5-to-json as a workaround, e.g.
image=$(json5-to-json <.devcontainer/devcontainer.json |jq -r '.image')
I'm sure this doesn't address all use cases, but it suits our needs well enough.