我想将 json 转换为新行分隔的 json。我尝试在 bash 中使用 jq 多次执行此操作,但我无法接近最终输出。
输入:
{
"windows124": {
"updated": "2015-01-14",
"attribution": [],
"description": "",
"notes": [],
"alt_names": [],
"sources": [],
"urls": ["google.com", "google.co.uk"],
"common_name": "test",
"uuid": "7259334c-3218-4259-aaab-896d87507f4f"
},
"linux124": {
"updated": "",
"attribution": ["Naifdddkoscn"],
"description": "",
"notes": [],
"alt_names": [],
"sources": [],
"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],
"common_name": "121212",
"uuid": "009db412-762d-4256-8df9-eb213be01ffd"
},
"wikipedia123": {
"updated": "2018-07-31",
"attribution": [],
"description": "",
"notes": [],
"alt_names": [],
"sources": [],
"urls": ["https://example.com/1.pdf"],
"common_name": "test343",
"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0"
}
}
想要的输出:
{"uuid": "7259334c-3218-4259-aaab-896d87507f4f","family": "windows124","updated": "2015-01-14","attribution": [],"description": "","notes": [],"alt_names": [],"sources": [],"urls": ["google.com", "google.co.uk"],"common_name": "test"}
{"uuid": "009db412-762d-4256-8df9-eb213be01ffd","family": "linux124", "updated": "","attribution": ["Naifdddkoscn"],"description": "","notes": [],"alt_names": [],"sources": [],"urls": ["https://example.com/1.pdf", "https://example.com/1.pdf", "https://example.com/1.pdf"],"common_name": "121212"}
{"uuid": "4d8da0af-cfd7-4990-b211-af0e990vfda0","family": "wikipedia123", "updated": "2018-07-31","attribution": [],"description": "","notes": [],"alt_names": [],"sources": [],"urls": ["https://example.com/1.pdf"],"common_name": "test343"}
到目前为止我所拥有的是:cat deserialize.json | jq -c '.|to_entries[]'
{"key":"windows124","value":{"updated":"2015-01-14","attribution":[],"description":"","notes":[],"alt_names":[],"sources":[],"urls":["google.com","google.co.uk"],"common_name":"test","uuid":"7259334c-3218-4259-aaab-896d87507f4f"}}
{"key":"linux124","value":{"updated":"","attribution":["Naifdddkoscn"],"description":"","notes":[],"alt_names":[],"sources":[],"urls":["https://example.com/1.pdf","https://example.com/1.pdf","https://example.com/1.pdf"],"common_name":"121212","uuid":"009db412-762d-4256-8df9-eb213be01ffd"}}
{"key":"wikipedia123","value":{"updated":"2018-07-31","attribution":[],"description":"","notes":[],"alt_names":[],"sources":[],"urls":["https://example.com/1.pdf"],"common_name":"test343","uuid":"4d8da0af-cfd7-4990-b211-af0e990vfda0"}}
答案1
您可以使用此 jq 过滤器:
<file jq 'to_entries|map(.value + {family:(.key)})[]'
正如您所发现的,该to_entries
函数允许获取键名称以添加属性family
。
所以过滤器只创建这个对象并将其添加到函数给定family
的内容中。value
to_entries
该map
函数对数组的所有元素执行添加操作value
。
最后[]
去掉了外部数组。
请注意,输出的顺序与您发布的方式不同,但内容是相同的。如果您希望对键进行排序,请使用选项-S
。
答案2
jq -c 'with_entries(.value.family = .key)[]' file.json
或者,
jq -c 'with_entries(.value += { family: .key })[]' file.json
这会将一个family
键插入到原始顶级对象的每个子元素中,并带有该元素的键值(例如windows124
第一个元素)。然后它返回子元素作为一个集合,这正是您想要的。
问题中给出的文档的输出:
{"updated":"2015-01-14","attribution":[],"description":"","notes":[],"alt_names":[],"sources":[],"urls":["google.com","google.co.uk"],"common_name":"test","uuid":"7259334c-3218-4259-aaab-896d87507f4f","family":"windows124"}
{"updated":"","attribution":["Naifdddkoscn"],"description":"","notes":[],"alt_names":[],"sources":[],"urls":["https://example.com/1.pdf","https://example.com/1.pdf","https://example.com/1.pdf"],"common_name":"121212","uuid":"009db412-762d-4256-8df9-eb213be01ffd","family":"linux124"}
{"updated":"2018-07-31","attribution":[],"description":"","notes":[],"alt_names":[],"sources":[],"urls":["https://example.com/1.pdf"],"common_name":"test343","uuid":"4d8da0af-cfd7-4990-b211-af0e990vfda0","family":"wikipedia123"}