【问题标题】:unable to parse json into csv using jq无法使用 jq 将 json 解析为 csv
【发布时间】:2023-03-31 18:46:01
【问题描述】:

我有一个 JSON 文件,我想使用 shell 脚本中的 jq 将其转换为 CSV 文件。我想从整个 JSON 文件中创建一行。我必须从价值中提取价值。行输出应该类似于

null,642,642,412,0,null,null

Here is my JSON file

{
     "data": [
   {
     "name": "exits",
     "period": "lifetime",
     "values": [
       {
         "value": {}
       }
     ],
     "title": "Exits",
     "description": "Number of times someone exited the carousel"
   },
   {
     "name": "impressions",
     "period": "lifetime",
     "values": [
       {
         "value": 642
       }
     ],
     "title": "Impressions",
     "description": "Total number of times the media object has been seen"
   },
   {
     "name": "reach",
     "period": "lifetime",
     "values": [
       {
         "value": 412
       }
     ],
     "title": "Reach",
     "description": "Total number of unique accounts that have seen the media object"
   },
   {
     "name": "replies",
     "period": "lifetime",
     "values": [
       {
         "value": 0
       }
     ],
     "title": "Replies",
     "description": "Total number of replies to the carousel"
   },
   {
     "name": "taps_forward",
     "period": "lifetime",
     "values": [
       {
         "value": {}
       }
     ],
     "title": "Taps Forward",
     "description": "Total number of taps to see this story's next photo or video"
   },
   {
     "name": "taps_back",
     "period": "lifetime",
     "values": [
       {
         "value": {}
       }
     ],
     "title": "Taps Back",
     "description": "Total number of taps to see this story's previous photo or video"
   }
 ]
}

您好尝试使用这个 jq 命令: .data | map(.values[].value) | @csv

这给出了以下输出: jq:错误(在:70):对象({})在 csv 行中无效 退出状态 5

所以当我得到这个空的 JSON 对象时,它反映了一个错误。

请帮忙!!

行输出应该类似于

null,642,642,412,0,null,null

【问题讨论】:

    标签: bash shell jq


    【解决方案1】:

    在这里使用length==0 充其量是可疑的。要检查 {},可以这样写:

    jq '.data | map(.values[].value | if . == {} then "null" else . end) | @csv'
    

    同样适用于 []。

    【讨论】:

      【解决方案2】:

      如果你在没有@csv 部分的情况下运行命令,你会看到输出是:

      [
        {},
        642,
        412,
        0,
        {},
        {}
      ]
      

      通过将空对象替换为“null”:(length == 0)

      jq '.data | map(.values[].value) | map(if (type == "object" and length == 0 ) then "null" else . end) | @csv'
      
      Output:
      "\"null\",642,412,0,\"null\",\"null\""
      

      根据@aaron 的建议(见评论)。以下可以生成请求的输出而无需额外的后处理。免责声明:这不适用于我的 jq 1.5,但适用于 jqplay 和 jq 1.6。

      jq --raw-output '.data | map(.values[].value) | map(if (type == "object" and length == 0 ) then "null" else . end) | join(",")'
      
      Output:
      null,642,412,0,null,null
      

      【讨论】:

      • 要处理我建议使用| join(",")-r/--raw-output 而不是csv 过滤器的引号:jqplay.org/s/3Z9coUQlTz;也就是说,在字符串值周围加上引号在 CSV 的上下文中可以正常工作,其中可以引用字段以防它们可能包含字段分隔符的出现
      • @Aaron 你能输入完整的命令吗?我已经按照你的建议尝试了 append '@join...',我得到了字符串 (",") 和数字 (642) 无法添加
      • jq --raw-output '.data | map(.values[].value) | map(if (length == 0 ) then "null" else . end) | join(",")',如果您检查了 jqplay 链接,它会在底部提供完整的命令 :)
      • @Aaron 我将嵌入我的答案中,TY
      • 谢谢,伙计们。它有帮助。是否可以在结果为 0 的地方打印 0 而在 json 对象为空白的地方打印 null ?
      猜你喜欢
      • 2019-12-13
      • 1970-01-01
      • 2018-08-29
      • 1970-01-01
      • 2016-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多