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

JSON不会在Unix中使用jq进行转换,json,bash,unix,jq,Json,Bash,Unix,Jq,转换此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_na

转换此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:
这是其中一个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对象流,那么有效的解决方案将使用
inputs
和-n命令行选项。这可以大致如下:

print_header,
(inputs | print_row)

该脚本可以正常工作,我还将介绍实用程序函数。第一个解决方案复制多行JSON中每个条目的标题,这是预期的吗?您必须根据您的数据调整给定的程序,可能使用
.table.All[]
.table.All[]返回“不能在null(null)上迭代”,尽管您没有显示原始JSON,您必须自己根据数据调整程序,但请参见更新答案中的JSONL部分。
print_header,
(inputs | print_row)