繁体   English   中英

JSON 不会在 Unix 中使用 jq 进行转换

[英]JSON will not convert with jq in Unix

转换此 JSON 时遇到困难。 它是多行的,类似于下面的内容。 底部的示例数据是解压缩后读取的内容。

已尝试的示例:

jq -r '(([["user_id","server_received_time","app","device_carrier","$schema","city","uuid","event_time","platform","os_version","amplitude_id","processed_time","user_creation_time","version_name","ip_address","paying","dma","group_properties","user_properties","client_upload_time","$insert_id","event_type","library","amplitude_attribution_ids","device_type","device_manufacturer","start_version","location_lng","server_upload_time","event_id","location_lat","os_name","amplitude_event_type","device_brand","groups","event_properties","data","device_id","language","device_model","country","region","is_attribution_event","adid","session_id","device_family","sample_rate","idfa","client_event_time"]]) + [(.table.All[] | [.user_id,.server_received_time,.app,.device_carrier,.$schema,.city,.uuid,.event_time,.platform,.os_version,.amplitude_id,.processed_time,.user_creation_time,.version_name,.ip_address,.paying,.dma,.group_properties,.user_properties,.client_upload_time,.$insert_id,.event_type,.library,.amplitude_attribution_ids,.device_type,.device_manufacturer,.start_version,.location_lng,.server_upload_time,.event_id,.location_lat,.os_name,.amplitude_event_type,.device_brand,.groups,.event_properties,.data,.device_id,.language,.device_model,.country,.region,.is_attribution_event,.adid,.session_id,.device_family,.sample_rate,.idfa,.client_event_time])])[]|@csv' test.json > test.csv

以及其他一些 jq 选项。 无论值如何,我都需要每一列,以及原样的值。 有没有人想过我们为什么会遇到问题? 我们得到的一个错误是:

jq: error: try .["field"] instead of .field for unusually named fields at <top-level>, line 1:

其他 jq 行给出了以下错误:

string (...) cannot be csv-formatted, only array

这是 JSON 文件之一的摘录:

{"groups":{},"country":"United States","device_id":"3d-88c-45-b6-ed81277eR","is_attribution_event":false,"server_received_time":"2019-12-17 17:29:11.113000","language":"English","event_time":"2019-12-17 17:27:49.047000","user_creation_time":"2019-11-08 13:15:32.919000","city":"Sure","uuid":"someID","device_model":"Windows","amplitude_event_type":null,"client_upload_time":"2019-12-17 17:29:21.958000","data":{},"library":"amplitude-js\/5.2.2","device_manufacturer":null,"dma":"Washington, DC (Townville, USA)","version_name":null,"region":"Virginia","group_properties":{},"location_lng":null,"device_family":"Windows","paying":null,"client_event_time":"2019-12-17 17:27:59.892000","$schema":12,"device_brand":null,"user_id":"email@gmail.com","event_properties":{"title":"Name","id":"1-253251","applicationName":"SomeName"},"os_version":"18","device_carrier":null,"server_upload_time":"2019-12-17 17:29:11.135000","session_id":1576603675620,"app":231165,"amplitude_attribution_ids":null,"event_type":"CHANGE_PERSPECTIVE","user_properties":{},"adid":null,"device_type":"Windows","$insert_id":"e308c923-d8eb-48c6-8ea5-600","event_id":24,"amplitude_id":515,"processed_time":"2019-12-17 17:29:12.760372","platform":"Web","idfa":null,"os_name":"Edge","location_lat":null,"ip_address":"123.456.78.90","sample_rate":null,"start_version":null}

谢谢!

你的尝试有几个问题。

首先,不能使用缩写的.foo语法指定名称中带有“$”的键; 你可以使用.["$foo"]代替。

其次, @csv需要一个原子值数组。 因此,必须特别处理以 JSON 对象作为值的键。

第三,“+”不正确。 这里的相关连接符是“,”。

使用您的示例 JSON,以下内容将起作用:

(["user_id","server_received_time","app","device_carrier","$schema","city","uuid","event_time","platform","os_version","amplitude_id","processed_time","user_creation_time","version_name","ip_address","paying","dma","group_properties","user_properties","client_upload_time","$insert_id","event_type","library","amplitude_attribution_ids","device_type","device_manufacturer","start_version","location_lng","server_upload_time","event_id","location_lat","os_name","amplitude_event_type","device_brand","groups","event_properties","data","device_id","language","device_model","country","region","is_attribution_event","adid","session_id","device_family","sample_rate","idfa","client_event_time"]),

([.user_id,.server_received_time,.app,.device_carrier,.["$schema"],.city,.uuid,.event_time,.platform,.os_version,.amplitude_id,.processed_time,.user_creation_time,.version_name,.ip_address,.paying,.dma,.group_properties,.user_properties,.client_upload_time,.["$insert_id"],.event_type,.library,.amplitude_attribution_ids,.device_type,.device_manufacturer,.start_version,.location_lng,.server_upload_time,.event_id,.location_lat,.os_name,.amplitude_event_type,.device_brand,.groups,.event_properties,.data,.device_id,.language,.device_model,.country,.region,.is_attribution_event,.adid,.session_id,.device_family,.sample_rate,.idfa,.client_event_time]
 | map(if type=="object"
       then to_entries
       | map( "\(.key):\(.value)" )
       | join(";")
       else . end))
| @csv

不易出错的解决方案

两次指定长长的键列表会使上述解决方案容易出错。 最好只指定一次键,然后以编程方式生成行。

这是一个可用于此目的的实用程序函数:

def toa($headers):
  . as $in | $headers | map($in[.]);

或者您可以处理toa的对象值键:

def toa($headers):
  def flat: 
     if type == "object" or type == "array"
     then to_entries | map( "\(.key):\(.value)" ) | join(";") 
     else .
     end;
  . as $in | $headers | map($in[.] | flat);

JSONL

如果输入是问题中所示类型的 JSON 对象流,则有效的解决方案将使用带有 -n 命令行选项的inputs 这可能是沿着以下路线:

print_header,
(inputs | print_row)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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