asby
asby

Reputation: 13

Reconstructing JSON with jq

I have a JSON like this (sample.json):

{
  "sheet1": [
    {
      "hostname": "sv001",
      "role": "web",
      "ip1": "172.17.0.3"
    },
    {
      "hostname": "sv002",
      "role": "web",
      "ip1": "172.17.0.4"
    },
    {
      "hostname": "sv003",
      "role": "db",
      "ip1": "172.17.0.5",
      "ip2": "172.18.0.5"
    }
  ],
  "sheet2": [
    {
      "hostname": "sv004",
      "role": "web",
      "ip1": "172.17.0.6"
    },
    {
      "hostname": "sv005",
      "role": "db",
      "ip1": "172.17.0.7"
    },
    {
      "hostname": "vsv006",
      "role": "db",
      "ip1": "172.17.0.8"
    }
  ],
  "sheet3": []
}

I want to extract data like this:

sheet1

jq '(something command)' sample.json

{
    "web": {
        "hosts": [
            "172.17.0.3",
            "172.17.0.4"
        ]
    },
    "db": {
        "hosts": [
            "172.17.0.5"
        ]
    }
}

Is it possible to perform the reconstruction with jq map? (I will reuse the result for ansible inventory.)

Upvotes: 0

Views: 218

Answers (2)

peak
peak

Reputation: 116670

Here's a short, straight-forward and efficient solution -- efficient in part because it avoids group_by by courtesy of the following generic helper function:

def add_by(f;g): reduce .[] as $x ({}; .[$x|f] += [$x|g]);
.sheet1
| add_by(.role; .ip1) 
| map_values( {hosts: .} )

Output

This produces the required output:

{
 "web": {
    "hosts": [
      "172.17.0.3",
      "172.17.0.4"
    ]
  },
  "db": {
    "hosts": [
      "172.17.0.5"
    ]
  }
}

Upvotes: 1

Jeff Mercado
Jeff Mercado

Reputation: 134811

If the goal is to regroup the ips by their roles within each sheet you could do this:

map_values(
    reduce group_by(.role)[] as $g ({};
        .[$g[0].role].hosts = [$g[] | del(.hostname, .role)[]]
    )
)

Which produces something like this:

{
  "sheet1": {
    "db": {
      "hosts": [
        "172.17.0.5",
        "172.18.0.5"
      ]
    },
    "web": {
      "hosts": [
        "172.17.0.3",
        "172.17.0.4"
      ]
    }
  },
  "sheet2": {
    "db": {
      "hosts": [
        "172.17.0.7",
        "172.17.0.8"
      ]
    },
    "web": {
      "hosts": [
        "172.17.0.6"
      ]
    }
  },
  "sheet3": {}
}

https://jqplay.org/s/3VpRc5l4_m

If you want to flatten all to a single object keeping only unique ips, you can keep everything mostly the same, you'll just need to flatten the inputs prior to grouping and remove the map_values/1 call.

$ jq -n '
reduce ([inputs[][]] | group_by(.role)[]) as $g ({};
    .[$g[0].role].hosts = ([$g[] | del(.hostname, .role)[]] | unique)
)
'
{
  "db": {
    "hosts": [
      "172.17.0.5",
      "172.17.0.7",
      "172.17.0.8",
      "172.18.0.5"
    ]
  },
  "web": {
    "hosts": [
      "172.17.0.3",
      "172.17.0.4",
      "172.17.0.6"
    ]
  }
}

https://jqplay.org/s/ZGj1wC8hU3

Upvotes: 0

Related Questions