on-internet
← Community

Flatten JSON with jq retaining key names

Stack Overflow ·

I'm trying to flatten a JSON consisting of nested objects. The top layer contains several key/value pairs, where each value is itself an array of a number of objects (the bottom layer). What I would like to get, using jq, is simply an array of objects containing all the objects of the bottom layer, each of which with an additional key/value pair identifying the top-layer key it originally belonged to. In other words, I would like to turn a JSON { "key1": [obj1, obj2], "key2": [obj3] } into a plain array [OBJ1, OBJ2, OBJ3] where each OBJi is simply the original object with an extra key/value pa

Comments

Sign in or sign up to comment and upvote.

No comments yet.