无法按要求组建JQ

问题描述 投票:0回答:1

我正在使用JQ Play。

这是我的 JSON 数据 -

{
"body":{
"primarykey": [
    "book_id",
    "edition_id"
],
"members": [
    {
        "name": "book_id",
        "type": "NUMBER"
    },
    {
        "name": "edition_id",
        "type": "NUMBER"
    },
    {
        "name": "title",
        "type": "VARCHAR2"
    }
],
"links": [
    {
        "rel": "collection",
        "href": "https://tdcvm28s16.bcone.com:7002/ords/testords/metadata-catalog/",
        "mediaType": "application/json"
    },
    {
        "rel": "canonical",
        "href": "https://tdcvm28s16.bcone.com:7002/ords/testords/metadata-catalog/books/",
        "mediaType": "application/json"
    },
    {
        "rel": "alternate",
        "href": "https://tdcvm28s16.bcone.com:7002/ords/testords/open-api-catalog/books/",
        "mediaType": "application/openapi+json"
    },
    {
        "rel": "describes",
        "href": "https://tdcvm28s16.bcone.com:7002/ords/testords/books/"
    }
]
}
}

我的JQ-

.body.primarykey[] | "{" + . + "},"

电流输出-

"{book_id},"
"{edition_id},"

我的预期输出是-

{"book_id"},
{"edition_id"}

这里如果有更多主键,它们也应该被附加到预期输出中。

请帮助我找到可以在 jqplay.org 中使用的正确 JQ

json jq
1个回答
0
投票

如果您可以接受尾随逗号:

jq -r '.body.primarykey[] | "{\"\(.)\"},"'

没有结尾的逗号:

jq -r '.body.primarykey | map("{\"\(.)\"}") | join(",\n")'
© www.soinside.com 2019 - 2024. All rights reserved.