Given:
{"id": 1, "date": "2014-12-30", "history":[{"id":1, "date":"2014-12-30", "type":"open"},{"id":2, "date":"2014-12-31", "type":"close"}]}
I am attempting to flatten the data for import into RDBMS tables (via the @csv operator), with each object in the "history" array (with 1 or more array elements) as a separate line with the parent fields duplicated, as in:
{"id": 1, "date": '"2014-12-30", "id":1, "date":"2014-12-30', "type":"open"}
{"id": 1, "date": '"2014-12-30", "id":2, "date":"2014-12-31', "type":"close"}
eventually becoming
1, 2014-12-30, 1, 2014-12-30, "open"
1, 2014-12-30, 2, 2014-12-31, "close"
I see a flatten command coming in jq 1.5, but not sure if it or another method is already available in jq 1.4. Noting the "id" field is duplicated in name (as with the data I am receiving), I could either deal with it by position, or accept a prefix assigned by jq.
Is there a generic pattern in jq to flatten a 1-level nested array like this?
After fixing up the JSON:
jq '. as $in | .history[] | . as $h | [$in.id, $in.date] + [$h[]]' 646.json
[
1,
"2014-12-30",
1,
"2014-12-30",
"open"
]
[
1,
"2014-12-30",
2,
"2014-12-31",
"close"
]
And thus:
jq -r '. as $in | .history[] | . as $h | [$in.id, $in.date] + [$h[]]|@csv' 646.json
1,"2014-12-30",1,"2014-12-30","open"
1,"2014-12-30",2,"2014-12-31","close"
That works perfect. Many thanks.
Most helpful comment
After fixing up the JSON:
And thus: