Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 527

Using jq, how to transform a nested data structure into a "flat" command-like sequence

$
0
0

I have the following JSON structure that represents a database schema:

{"db": [    {"tables": [        {"name": "tblFoo","cols": [            {"name": "created","types": [ { "data_type": "timestamp" } ]            },            {"name": "updated","types": [ { "data_type": "timestamp" } ]            },            {"name": "username","types": [ { "data_type": "timestamp" } ]            }          ]        },        {"name": "tblBar","cols": [            {"name": "created","types": [ { "data_type": "timestamp" } ]            },…

etc., you get the idea.I need it transformed to a sequential set of json "commands" where every table and its columns is a separate line, like so:

[{"type": "TABLE", name:"tblFoo"},{"type": "COLUMN", name:"created", ofTable: "tblFoo"},{"type": "COLUMN", name:"updated", ofTable: "tblFoo"},…{"type": "TABLE", name:"tblBar"},{"type": "COLUMN", name:"created", ofTable: "tblBar"},]

I just can't wrap my head around it. I've tried .db.tables[] | map(…) at first but this obviously only creates an array of the first level, tables, and I can't add/inject the column definitions into the same level.

Then I've tried iterating over the columns like .db.tables[].cols[] with the recursive decent operator, but again the resulting list is only those of the columns and I fail to add/inject the table ones into the outgoing list.

Can you help me please getting on the right track?


Viewing all articles
Browse latest Browse all 527

Trending Articles