简体   繁体   中英

convert json to csv using jq bash

have a JSON data like below

"metric": {
   "name" : "name1"
},
"values": [
 [
   16590879,
   "0.043984349"
],
"values": [
 [
   16590876,
   "0.043983444"
]
]

} }

writing below jq, but not giving proper result

jq -r '[.metric.name,(.values[] | map(.) | @csv)'

Actual result

[
"name1",
"16590879",\"0.043984349\"",
"16590876",\"0.043983444\"",
"16590874",\"0.043934345\""

Expected result

name1,16590879,0.043984349
name1,16590876,0.043983444
name2,16590874,0.043934345

The sample data provided is invalid as JSON, but assuming it has been adjusted as shown below, we would have:

< sample.json  jq -r '[.metric.name] + .values[] | @csv'
"name1",16590879,"0.043984349"
"name1",16590876,"0.043983444"

If you don't want the quotation marks, then use join(",") instead of @csv .


sample.json

{
  "metric": {
    "name": "name1"
  },
  "values": [
    [
      16590879,
      "0.043984349"
    ],
    [
      16590876,
      "0.043983444"
    ]
  ]
}

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