0

I have some json data that looks like:

{
    "a1": {
        "b1": {
            "c1": {...},
            "c2": [...]
        },
        "b2": {
            "c3": {...},
            "c4": [...]
        }
    },
    "a2": {
        "b3": {
            "c5": {...},
            "c6": [...]
        },
        "b4": {
            "c7": {...},
            "c8": [...]
        }
    }
}

I want to convert it to:

{
    "a1": {
        "b1": {},
        "b2": {}
    },
    "a2": {
        "b3": {},
        "b4": {}
    }
}

Note that bs are empty objects. I can get a list of bs as arrays:

cat testjq.json | jq '. | map_values(keys)'

But can't figure out how to map it to objects.

peak
  • 105,803
  • 17
  • 152
  • 177
dmay
  • 1,340
  • 8
  • 23

1 Answers1

0

If you just want to set all items on second level to an empty object, use:

jq '.[][] = {}' some.json
{
  "a1": {
    "b1": {},
    "b2": {}
  },
  "a2": {
    "b3": {},
    "b4": {}
  }
}

Demo

Note: map_values is defined as def map_values(f): .[] |= f;, so .[][] = {} is effectively the same as map_values(map_values({})).

pmf
  • 24,478
  • 2
  • 22
  • 31