Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jo: option to dedup object keys #143

Merged
merged 1 commit into from
Mar 17, 2021
Merged

Conversation

gromgit
Copy link
Collaborator

@gromgit gromgit commented Mar 17, 2021

The JSON spec doesn't say anything about duplicate object keys, but many implementations either choke on them or quietly keep only the last value of each key. This PR maintains the jo default (duplicates preserved), but adds the -D option to deduplicate.

$ jo a=1 b=2 a=3
{"a":1,"b":2,"a":3}

$ jo -D a=1 b=2 a=3
{"a":3,"b":2}

$ jo -d . stage=$(jo 1=a 2=b 3=c) down=up stage.2=x stage\[3\]=y stage.4=d stage\[1\]=h
{"stage":{"1":"a","2":"b","3":"c","2":"x","3":"y","4":"d","1":"h"},"down":"up"}

$ jo -D -d . stage=$(jo 1=a 2=b 3=c) down=up stage.2=x stage\[3\]=y stage.4=d stage\[1\]=h
{"stage":{"1":"h","2":"x","3":"y","4":"d"},"down":"up"}

The JSON spec doesn't say anything about duplicate object keys, but many implementations either choke on them or quietly keep only the last value of each key. This PR maintains the `jo` default (duplicates preserved), but adds the `-D` option to deduplicate.

```bash
$ jo a=1 b=2 a=3
{"a":1,"b":2,"a":3}

$ jo -D a=1 b=2 a=3
{"a":3,"b":2}

$ jo -d . stage=$(jo 1=a 2=b 3=c) down=up stage.2=x stage\[3\]=y stage.4=d stage\[1\]=h
{"stage":{"1":"a","2":"b","3":"c","2":"x","3":"y","4":"d","1":"h"},"down":"up"}

$ jo -D -d . stage=$(jo 1=a 2=b 3=c) down=up stage.2=x stage\[3\]=y stage.4=d stage\[1\]=h
{"stage":{"1":"h","2":"x","3":"y","4":"d"},"down":"up"}
```
@jpmens jpmens merged commit f60d02f into jpmens:master Mar 17, 2021
@jpmens
Copy link
Owner

jpmens commented Mar 17, 2021

You are busy here this week! Thank you very much. Merged!

@gromgit gromgit deleted the insert_keys branch March 17, 2021 15:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants