简体   繁体   English

如何使用JQ从json对象打印keyS

[英]How do I print the keyS from a json object using JQ

Sample input 样本输入

{
 “event_timestamp”: “2016-03-16 13:19:53 UTC”,
 “query”: “Plagiarism”,
 “search_session_id”: “3605862756e95d26ac180",
 “version”: “0.0.2",
 “other”: “{\“client_timestamp\“:1458134393.932,\"ios_page_index\":3}“,
 “action”: “HIT_BOUNCE”
}

{
 “event_timestamp”: “2016-03-16 13:19:53 UTC”,
 “query”: “Plagiarism”,
 “search_session_id”: “3605862756e95d26ac180",
 “version”: “0.0.2",
 “other”:“{\“client_timestamp\“:1458134393.932,\"ios_page_index\":3,\"ios_index_path_row\":1}“,
 “action”: “HIT_BOUNCE”
}

I'd like to output the unique key name in "other" field 我想在“其他”字段中输出唯一的键名称

"client_timestamp, “ client_timestamp,

ios_page_index, ios_page_index,

ios_index_path_row " ios_index_path_row“

Tried the following command but doesn't work so far 尝试了以下命令,但到目前为止无法正常工作

cat sampleexample.json | 猫sampleexample.json | jq '.other|keys' | jq'.other | keys'| sort | 排序 uniq > other.json uniq> other.json

Thanks in advance 提前致谢

  1. The sample input is not JSON, which does not allow fancy quotes to be used as string delimiters. 输入的示例不是JSON,它不允许将奇特的引号用作字符串定界符。 The following assumes the input has been corrected. 以下假定输入已更正。

  2. The value of .other is a JSON string; .other的值是JSON字符串; you can use fromjson to change the string to a JSON object. 您可以使用fromjson将字符串更改为JSON对象。

  3. sort|unique is redundant, as unique first sorts its input. sort|unique是多余的,因为unique首先对其输入进行排序。

Putting it all together: 放在一起:

$ jq  '.other | fromjson | keys_unsorted | unique' input.json
[
  "client_timestamp",
  "ios_page_index"
]
[
  "client_timestamp",
  "ios_index_path_row",
  "ios_page_index"
]

(Using keys_unsorted saves one sort operation.) (使用keys_unsorted保存一种排序操作。)

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

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