json - 迭代项目 json 数组

标签 json jq export-to-csv

我想寻求帮助。 我有 json 数组。 如何在分割后迭代数组并创建 csv 输出


我的输入是...

[
    {
        "body": [
            {
                "title": "topinfo",
                "longText": "item1|details item 1|details sdfdfdfd ||details gbgfgghmhjmh||details 5348786"
            },
            {
                "title": "topinfo",
                "longText": "item2|details item 2|details sdfdfdfd ||details gbgfgghmhjmh||details 9784561"
            }
            ]
    }
]


我实际上呼吁 jq ...

jq '.[] | [.body[] | {longText,title} ] | to_entries | map( (.value.level = "\(1+.key)" ) | .value) | .[] | [.title,.level,(.longText|split("|"))] '


实际结果

[
  "topinfo",
  "1",
  [
    "item1",
    "details item 1",
    "details sdfdfdfd",
    "details gbgfgghmhjmh",
    "details 5348786"
  ]
]
[
  "topinfo",
  "2",
  [
    "item2",
    "details item 2",
    "details sdfdfdfd",
    "details gbgfgghmhjmh",
    "details 9784561"
  ]
]


我希望得到这个 csv...

topinfo;1;1;item1
topinfo;1;2;details item 1
topinfo;1;3;details sdfdfdfd
topinfo;1;4;details gbgfgghmhjmh
topinfo;1;5;details 5348786

topinfo;2;1;item2
topinfo;2;2;details item 2
topinfo;2;3;details sdfdfdfd
topinfo;2;4;details gbgfgghmhjmh
topinfo;2;5;details 9784561

最佳答案

嵌套迭代和变量绑定(bind):

.[].body                                                   # top-level iteration
| to_entries[] as {key: $i, value: {$title, $longText}}    # collect values
| $longText / "|"                                          # split at "|"
| map(select(. != ""))                                     # remove empty items(?)
| (                                                        # open grouping context
    to_entries[] as {key: $j, $value}                      # collect values
    | [$title, $i+1, $j+1, $value]                         # prepare output line
    | join(";")                                            # join to string
  ), ""                                                    # add empty line
topinfo;1;1;item1
topinfo;1;2;details item 1
topinfo;1;3;details sdfdfdfd 
topinfo;1;4;details gbgfgghmhjmh
topinfo;1;5;details 5348786

topinfo;2;1;item2
topinfo;2;2;details item 2
topinfo;2;3;details sdfdfdfd 
topinfo;2;4;details gbgfgghmhjmh
topinfo;2;5;details 9784561

Demo

要正确转义 CSV 输出,请使用 @csv 而不是 join(";")。请注意,这将通过逗号而不是分号“连接”,并在必要时引用这些项目:

"topinfo",1,1,"item1"
"topinfo",1,2,"details item 1"
"topinfo",1,3,"details sdfdfdfd "
"topinfo",1,4,"details gbgfgghmhjmh"
"topinfo",1,5,"details 5348786"

"topinfo",2,1,"item2"
"topinfo",2,2,"details item 2"
"topinfo",2,3,"details sdfdfdfd "
"topinfo",2,4,"details gbgfgghmhjmh"
"topinfo",2,5,"details 9784561"

Demo

关于json - 迭代项目 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76849848/

相关文章:

json - 使用 jq 将 JSON 数组转换为 CSV

json - 在 Ubuntu 上将 jq 升级到 1.5

python - 将数据写入 CSV 格式文件

r - 在R中将data.frames写入csv时如何命名多个csv文件?

javascript - 动态 json 回调函数仅检索最后一个值

javascript - 根据每个值的内容过滤掉 JSON 数组

python - 如何使用Python提取嵌套的JSON数据?

php - 服务器没有给出 json 输出

datetime - jq 解析日期到时间戳

angularjs - Angular ui-grid 外部导出按钮