jqを使用したJSON変換

jqを使用したJSON変換

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関数に提供された項目に追加します。valueto_entries

このmap関数は、配列内のすべての要素に対してプラス演算を実行しますvalue

最後に、[]外部配列が削除されました。

出力順序は投稿した方法とは異なりますが、内容は同じですので参考にしてください。キーを並べ替えるには、オプションを使用します-S

おすすめ記事