Last active
June 1, 2018 14:14
-
-
Save tst2005/17f65f6f77dbd3b7a27ce9394907ca5f to your computer and use it in GitHub Desktop.
how to convert json object to array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# wanted result: | |
# [ | |
# [ "foo", "bar", "buz", "xx" ], | |
# [ "a1", "b1", "c1", null ], | |
# [ "a2", "b2", "c2", null ], | |
# [ "a3", "b3", "c3", null ], | |
# [ "a4", "b4", "c4", "undef" ] | |
# ] | |
printf '[ | |
{ | |
"foo": "a1", | |
"bar": "b1", | |
"buz": "c1" | |
}, { | |
"foo": "a2", | |
"bar": "b2", | |
"buz": "c2" | |
}, { | |
"foo": "a3", | |
"bar": "b3", | |
"buz": "c3" | |
}, { | |
"xx" : "undef", | |
"foo": "a4", | |
"bar": "b4", | |
"buz": "c4" | |
} | |
]' | | |
jq ' { | |
"data": ., | |
"keys": (.|map(keys)|flatten(1)|unique) | |
} | | |
.data |= map( | |
to_entries|sort_by(.key)|map(.value)|flatten(1) | |
) | | |
[ [.keys|sort], .data ] | |
| flatten(1) | |
' | |
# 1) get the keys | |
# map(keys)|flatten(1)|unique | |
# map(keys)|flatten(1)|unique|sort | |
# 2) make a array of array with each value ... | |
# ? | |
# map( to_entries|sort_by(.key)|map(.value)|flatten(1) ) | |
# Issue: | |
# - how to fill non-existant cell with null value ? | |
# - how to get each value in a specific ordered keys list (original unsorted keys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment