简体   繁体   中英

JQ, convert CSV (parent child format) to JSON

I have a CSV file (parent child format). Is it possible to convert to JSON format by using JQ?

CSV file <pre>

id  parent_id   text
1   NULL        engine
2   1           exhaust
3   1           cooling
4   3           cooling fan
5   3           water pump
6   NULL        frame
7   6           wheels
8   6           brakes
9   8           brake calipers
10  6           cables
</Pre>

JSON file <Pre>

[
{
"id": "1",
"text": "engine",
"children": [
{
"id": "2",
"text": "exhaust",
"children": []
},
{
"id": "3",
"text": "cooling",
"children": [
{
"id": "4",
"text": "cooling fan",
"children": []
},
{
"id": "5",
"text": "water pump",
"children": []
}
]
}
]
},
{
"id": "6",
"text": "frame",
"children": [
{
"id": "7",
"text": "wheels",
"children": []
},
{
"id": "8",
"text": "brakes",
"children": [
{
"id": "9",
"text": "brake calipers",
"children": []
}
]
},
{
"id": "10",
"text": "cables",
"children": []
}
]
}
]
</Pre>

The following provides the basis for a complete solution to the stated problem. Instead of providing the redundant details about the children, it just provides their ids. If you really need the redundancy, then simply add one more post-processing step, which is trivial to do.

The solution here is based on the construction of a dictionary of "children".

Invocation

Along the lines of:

jq -R -f program.jq data.csv

Program

def obj($headers):
  . as $in
  | reduce range(0; $headers|length) as $i ({}; 
      .[$headers[$i]] = $in[$i]);

split(",") as $headers
| [ inputs
    | split(",")
    | map_values(if . == "NULL" then null else . end)
    | obj($headers) ]
| (reduce .[] as $row ({};
      if $row.parent_id
      then .[$row.parent_id] += [$row.id]
      else . end ) ) as $children
| map( del(.parent_id) | .children = ($children[.id] // []) )

Output

With the given input, the output begins as follows:

[
  {
    "id": "1",
    "text": "engine",
    "children": [
      "2",
      "3"
    ]
  },
  {
    "id": "2",
    "text": "exhaust",
    "children": []
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM