-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Unexpected behavior with duplicate attributes #1795
Comments
jq uses a hash table to represent objects, and does not allow duplicates. You can actually handle duplicates in input using the |
@asarkar - As you say:
And indeed jq does allow them (no error message is generated), and one interpretation of the fact that the JSON spec discourages duplicate keys is that it is recognized that JSON parsers are free to interpret them as they see fit. None of this is to say that the jq documentation could not be improved! |
@nicowilliams I'm not a jq Ninja, can you show me a working command using the example in my post? |
@pkoppstein I stand corrected. Section 4:
|
asarkar wrote:
From the jq FAQ: 𝑸: Can jq process objects with duplicate keys? Can jq help convert objects with duplicate keys to an alternative format so that no information is lost? A: The JSON syntax formally allows objects with duplicate keys, and jq can accordingly read them, but the regular jq parser effectively ignores all but the last occurrence of each key within any given object. |
@asarkar sure, check this out:
In streaming mode you get a representation of the input as path + value tuples. "Closing" of arrays/objects is denoted as a tuple of just a path, which allows you to disambiguate inputs like the above example's. This means you can have as many duplicate keys as you'd like, and you'll see each and every one, but the price you pay is that for some tasks it's a bit harder to work with this representation. There's a few utility builtin functions for dealing with streamed JSON data ( Without knowing what you're trying to accomplish, I can only show you what jq can do, not how to apply it to your problem. |
@nicowilliams Thanks, I'm simply looking for something like |
It should be possible to write a version of |
@asarkar here:
Now you may want to unwrap some of the arrays:
|
We might want to add |
@asarkar did that work for you? Anyways, I'll integrate this into the next release -- it's a pretty nifty feature, so thanks for asking for it. |
@nicowilliams I didn't get around to trying it. As you can probably tell, the actual JSON is more complicated than the puny one I'd posted, and I didn't feel like jostling with jq on top of the already-quite-complicated code that you'd kindly provided. I ended up using a Groovy parser in Groovy Console. |
Fair enough. They complicated code i pasted is really just a small variation on code already inside jq, fyi :) |
@nicowilliams I thought you were going to leave this ticket open for ^^^ |
This commit adds a pair of built-ins for dealing with streamed JSON texts that have duplicate keys. Eventually we may need a `tostream_with_dups` as well that implements a convention that array values in objects are to be presented as duplicate keys with array elements as values. There are ambiguity issues, like how should one stream `{"a":[]}`.
This commit adds a pair of built-ins for dealing with streamed JSON texts that have duplicate keys. Eventually we may need a `tostream_with_dups` as well that implements a convention that array values in objects are to be presented as duplicate keys with array elements as values. There are ambiguity issues, like how should one stream `{"a":[]}`.
Did this ever happen? |
There's an open PR for that, but IIRC it's not ready. You can write such utility functions yourself in your jq programs. |
Actual behavior:
Duplicate keys are valid, but discouraged, in JSON. https://dzone.com/articles/duplicate-keys-in-json-objects
Expected behavior:
Both
b
andc
are output.Environment:
The text was updated successfully, but these errors were encountered: