如何仅使用jq将嵌套的JSON转换为CSV [英] How to convert nested JSON to CSV using only jq

查看:99
本文介绍了如何仅使用jq将嵌套的JSON转换为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注json

{
    "A": {
        "C": {
            "D": "T1",
            "E": 1
        },
        "F": {
            "D": "T2",
            "E": 2
        }
    },
    "B": {
        "C": {
            "D": "T3",
            "E": 3
        }
    }
}

我想按如下方式将其转换为csv,

I want to convert it into csv as follows,

A,C,T1,1
A,F,T2,2
B,C,T3,3

输出说明:父键将一直打印到我到达叶子孩子为止.当我到达叶子孩子时,打印它的值.

Description of output: The parents keys will be printed until, I've reached the leaf child. Once I reached leaf child, print its value.

我尝试了以下操作,但无法成功

I've tried following and couldn't succeed,

cat my.json | jq -r'(map(keys)| add | unique)为$ cols | map(.as $ row | $ cols | map($ row [.]))as $ rows | $ rows [] | @csv'

cat my.json | jq -r '(map(keys) | add | unique) as $cols | map(. as $row | $cols | map($row[.])) as $rows | $rows[] | @csv'

,这使我犯了一个错误.

and it throwing me an error.

我无法对父键进行硬编码,因为实际的json有太多记录.但是json的结构是相似的.我想念什么?

I can't hardcode the parent keys, as the actual json has too many records. But the structure of the json is similar. What am I missing?

推荐答案

有些要求不清楚,但是以下解决了该问题的一种解释:

Some of the requirements are unclear, but the following solves one interpretation of the problem:

paths as $path
| {path: $path, value: getpath($path)}
| select(.value|type == "object" )
| select( [.value[]][0] | type != "object")
| .path + ([.value[]])
| @csv

(可以优化该程序,但此处的演示旨在使各个步骤变得清晰明了.)

(This program could be optimized but the presentation here is intended to make the separate steps clear.)

调用:

jq -r -f leaves-to-csv.jq input.json

输出:

"A","C","T1",1
"A","F","T2",2
"B","C","T3",3

不带引号的字符串

为避免在字符串两边加上引号,您可以将上述管道的最后一个部分替换为:

Unquoted strings

To avoid the quotation marks around strings, you could replace the last component of the pipeline above with:

join(",")

这篇关于如何仅使用jq将嵌套的JSON转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆