我正在尝试仅从JSON文件中选择特定字段及其完整路径(结果来自Elasticsearch)。
我的JSON文件:
entryComponents
我正在将密钥转换为一个衬里:
{
"_index": "ships",
"_type": "doc",
"_id": "c36806c10a96a3968c07c6a222cfc818",
"_score": 0.057158414,
"_source": {
"user_email": "admin@example.com",
"current_send_date": 1552557382,
"next_send_date": 1570798063,
"data_name": "atari",
"statistics": {
"game_mode": "engineer",
"opened_game": 0,
"user_score": 0,
"space_1": {
"ship_send_priority": 10,
"ssl_required": "true",
"ship_send_delay": 15,
"user_score": 0,
"template1": {
"current_ship_status": "sent",
"current_ship_date": "4324242",
"checked_link_before_clicked": 0
},
"template2": {
"current_ship_status": "sent",
"current_ship_date": "4324242",
"checked_payload": 0
}
}
}
}
}
比起将输出传递给grep提取我想要的所有字段:
<file jq -c 'paths(scalars) as $p | [$p, getpath($p)]'
[["_index"],"ships"]
[["_type"],"doc"]
[["_id"],"c36806c10a96a3968c07c6a222cfc818"]
[["_score"],0.057158414]
[["_source","user_email"],"admin@example.com"]
[["_source","current_send_date"],1552557382]
[["_source","next_send_date"],1570798063]
[["_source","data_name"],"atari"]
[["_source","statistics","game_mode"],"engineer"]
[["_source","statistics","opened_game"],0]
[["_source","statistics","user_score"],0]
[["_source","statistics","space_1","ship_send_priority"],10]
[["_source","statistics","space_1","ssl_required"],"true"]
[["_source","statistics","space_1","ship_send_delay"],15]
[["_source","statistics","space_1","user_score"],0]
[["_source","statistics","space_1","template1","current_ship_status"],"sent"]
[["_source","statistics","space_1","template1","current_ship_date"],"4324242"]
[["_source","statistics","space_1","template1","checked_link_before_clicked"],0]
[["_source","statistics","space_1","template2","current_ship_status"],"sent"]
[["_source","statistics","space_1","template2","current_ship_date"],"4324242"]
[["_source","statistics","space_1","template2","checked_payload"],0]
最后,我正在将grep的输出传递给sed并清理不需要的字符,而不会得到我想要的结果:
<file jq -c 'paths(scalars) as $p | [$p, getpath($p)]' | grep -e '"_index"\|current_send_date\|current_send_date\|ship_send_delay\|ship_send_priority\|current_ship_status'
[["_index"],"ships"]
[["_source","current_send_date"],1552557382]
[["_source","statistics","space_1","ship_send_priority"],10]
[["_source","statistics","space_1","ship_send_delay"],15]
[["_source","statistics","space_1","template1","current_ship_status"],"sent"]
[["_source","statistics","space_1","template2","current_ship_status"],"sent"]
我正在寻找一种更好的方法,至少不使用grep从jq中提取字段。我可以使用SED进行内容准备,但是我觉得必须有一种更好的方法来获取不希望使用grep的字段。我相信必须有一些select(.mykey | .mykey1 | .mykey2)可以完成此任务。
答案 0 :(得分:2)
使用join
和字符串插值(\(...)
):
$ jq -r 'paths(scalars) as $p | "\($p|join("."))=\(getpath($p))"' file
_index=ships
_type=doc
_id=c36806c10a96a3968c07c6a222cfc818
_score=0.057158414
_source.user_email=admin@example.com
_source.current_send_date=1552557382
_source.next_send_date=1570798063
_source.data_name=atari
_source.statistics.game_mode=engineer
_source.statistics.opened_game=0
_source.statistics.user_score=0
_source.statistics.space_1.ship_send_priority=10
_source.statistics.space_1.ssl_required=true
_source.statistics.space_1.ship_send_delay=15
_source.statistics.space_1.user_score=0
_source.statistics.space_1.template1.current_ship_status=sent
_source.statistics.space_1.template1.current_ship_date=4324242
_source.statistics.space_1.template1.checked_link_before_clicked=0
_source.statistics.space_1.template2.current_ship_status=sent
_source.statistics.space_1.template2.current_ship_date=4324242
_source.statistics.space_1.template2.checked_payload=0
实际上,如果您拥有最新版的jq,甚至不需要grep,请尝试以下操作:
(paths(scalars) | select(IN(.[];
"_index",
"current_send_data",
"ship_send_delay",
"ship_send_priority",
"current_ship_status"
))) as $p | "\($p|join("."))=\(getpath($p))"