Skip to content

Instantly share code, notes, and snippets.

@jeiea
Last active December 7, 2019 16:36
Show Gist options
  • Save jeiea/a6a132e3ff2006c7911070d0a28cd3a7 to your computer and use it in GitHub Desktop.
Save jeiea/a6a132e3ff2006c7911070d0a28cd3a7 to your computer and use it in GitHub Desktop.
It extracts json structure with parsing string json and picking first element of array
jq 'def r: walk(if type == "array" then if .[0] then [.[0]] else [] end else (fromjson? | r) // . end); r' a.json
# a.json
# {
# "array": [1,2,3],
# "nested_json": "{\"likes\": [1,2,3]}"
# }
#
# output
# {
# "array": [
# 1
# ],
# "nested_json": {
# "likes": [
# 1
# ]
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment