Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This JQ one-liner to pivot JSON data

Pivot JSON data

This one-liner pivots data from group>users or user>groups:

$ cat incoming.json \
  | jq 'map(. as $in | .users[] | . as $u | {user:$u, group:$in.group}) | group_by(.user) | map({user:.[0].user, groups: map(.group)})'

[
  {
    "user": "user-1",
    "groups": [
      "grp-1",
      "grp-3"
    ]
  },
  {
    "user": "user-2",
    "groups": [
      "grp-3"
    ]
  },
  {
    "user": "user-3",
    "groups": [
      "grp-1",
      "grp-2"
    ]
  }
]
[
{
"group": "grp-1",
"users": [
"user-1",
"user-3"
]
},
{
"group": "grp-2",
"users": [
"user-3"
]
},
{
"group": "grp-3",
"users": [
"user-1",
"user-2"
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.