繁体   English   中英

通过按键过滤数组,使用 jq 展平 JSON 文档

[英]Flatten a JSON document using jq by filtering an array by keys

我有一个 JSON 格式如下:

    {
  "subFields": [
    {
      "id": "question_1",
      "type": "radioGroup",
      "description": "Description1",
      "title": "title1",
      "subFields": [
        {
          "type": "radio",
          "label": "Yes",
          "value": 1
        },
        {
          "type": "radio",
          "label": "No",
          "value": 0
        },
        {
          "uiComponent": "SmallContent",
          "componentProps": {
            "text": "* If the answer to the above question is “Yes”, please contact the Support immediately."
          }
        }
      ]
    },
    {
      "uiComponent": "Spacer"
    },
    {
      "id": "question_2",
      "type": "radioGroup",
      "description": "Description2",
      "title": "Title2",
      "subFields": [
        {
          "type": "radio",
          "label": "Label - Value 1",
          "value": 1
        },
        {
          "type": "radio",
          "label": "Label - Value 2",
          "value": 2
        },
        {
          "type": "radio",
          "label": "Label - Value 3",
          "value": 3
        },
        {
          "type": "radio",
          "label": "Other",
          "value": 13,
          "subFields": [
            {
              "id": "question_2a",
              "type": "string",
              "condition": {
                "type": "BinaryExpression",
                "operator": "==",
                "left": {
                  "type": "Identifier",
                  "name": "question_2"
                },
                "right": {
                  "type": "Literal",
                  "value": 13
                }
              }
            }
          ]
        }
      ]
    },
    {
      "id": "question_2_b",
      "style": {
        "marginTop": "30px"
      },
      "type": "radioGroup",
      "description": "Description3",
      "title": "",
      "subFields": [
        {
          "type": "radio",
          "label": "Label - Radio 1",
          "value": 1
        },
        {
          "type": "radio",
          "label": "Label - Radio 2",
          "value": 2
        },
        {
          "type": "radio",
          "label": "Label - Radio 3",
          "value": 3
        }
      ]
    },
    {
      "uiComponent": "Spacer"
    },
    {
      "id": "question_3",
      "type": "radioGroup",
      "description": "Description3",
      "title": "Title3",
      "subFields": [
        {
          "type": "radio",
          "label": "Yes",
          "value": 1
        },
        {
          "type": "radio",
          "label": "No",
          "value": 0
        }
      ]
    },
    {
      "uiComponent": "Spacer"
    },
    {
      "condition": {
        "type": "BinaryExpression",
        "operator": "==",
        "left": {
          "type": "Identifier",
          "name": "signer_type"
        },
        "right": {
          "type": "Literal",
          "value": "entity"
        }
      },
      "subFields": [
        {
          "uiComponent": "Spacer"
        },
        {
          "id": "question_4",
          "type": "radioGroup",
          "description": "Description_4",
          "title": "Title_4",
          "subFields": [
            {
              "type": "radio",
              "label": "Yes",
              "value": 1
            },
            {
              "type": "radio",
              "label": "No",
              "value": 0
            }
          ]
        },
        {
          "uiComponent": "Spacer"
        }
      ],
      "uiComponent": "Block"
    },
    {
      "uiComponent": "Spacer"
    }
  ],
  "uiComponent": "Container"
}

我想生成以下 output:

[
    {
        "id": "question_1",
        "title": "title1",
        "description": "Description1",
        "type": "radioGroup",
        "questions": "radio,Yes,1"
    },
    {
        "id": "question_1",
        "title": "title1",
        "description": "Description1",
        "type": "radioGroup",
        "questions": "radio,No,0"
    },
    {
        "id": "question_2",
        "title": "Title2",
        "description": "Description2",
        "type": "radioGroup",
        "questions": "radio,Label - Value 1,1"
    },
    {
        "id": "question_2",
        "title": "Title2",
        "description": "Description2",
        "type": "radioGroup",
        "questions": "radio,Label - Value 2,2"
    },
    {
        "id": "question_2",
        "title": "Title2",
        "description": "Description2",
        "type": "radioGroup",
        "questions": "radio,Label - Value 3,3"
    },
    {
        "id": "question_2_b",
        "title": "",
        "description": "Description3",
        "type": "radioGroup",
        "questions": "radio,Label - Value 1,1"
    },
    {
        "id": "question_2_b",
        "title": "",
        "description": "Description3",
        "type": "radioGroup",
        "questions": "radio,Label - Value 2,2"
    },
    {
        "id": "question_2_b",
        "title": "",
        "description": "Description3",
        "type": "radioGroup",
        "questions": "radio,Label - Value 3,3"
    },
    {
        "id": "question_3",
        "title": "Title3",
        "description": "Description3",
        "type": "radioGroup",
        "questions": "radio,Yes,1"
    },
    {
        "id": "question_3",
        "title": "Title3",
        "description": "Description3",
        "type": "radioGroup",
        "questions": "radio,No,0"
    }
]

或替代一个简化版本:

[
  "question_1",
  "title1",
  "Description1",
  "radioGroup",
  "radio,Yes,1",
  "radio,No,0"
],
[
  "question_2",
  "title2",
  "Description2",
  "radioGroup",
  "radio,Label - Value 1,1",
  "radio,Label - Value 2,2",
  "radio,Label - Value 3,3",
],
[
  "question_2_b",
  "Description3",
  "radioGroup",
  "radio,Label - Value 1,1",
  "radio,Label - Value 2,2",
  "radio,Label - Value 3,3",
],
[
  "question_3",
  "Title3",
  "Description3",
  "radioGroup",
  "radio,Yes,1",
  "radio,No,0"
]

目标是仅获取包含 id 的对象(以删除{"uiComponent": "Spacer"}对象)并仅获取数组内具有这些标签的子字段

  "subFields": [
    {
      "type": "xxxx",
      "label": "xxxx",
      "value": xxxx
    },

通过使用以下 JQ 模式,我能够展平 JSON 阵列:

jq 玩 1

.subFields[] | select(has("id") and .id != null)| {id: .id, type: .type, description: .description, anwers: .subFields}

并产生了这个结果:

{
  "id": "question_1",
  "type": "radioGroup",
  "description": "Description1",
  "anwers": [
    {
      "type": "radio",
      "label": "Yes",
      "value": 1
    },
    {
      "type": "radio",
      "label": "No",
      "value": 0
    },
    {
      "uiComponent": "SmallContent",
      "componentProps": {
        "text": "* If the answer to the above question is “Yes”, please contact the Support immediately."
      }
    }
  ]
}
{
  "id": "question_2",
  "type": "radioGroup",
  "description": "Description2",
  "anwers": [
    {
      "type": "radio",
      "label": "Label - Value 1",
      "value": 1
    },
    {
      "type": "radio",
      "label": "Label - Value 2",
      "value": 2
    },
    {
      "type": "radio",
      "label": "Label - Value 3",
      "value": 3
    },
    {
      "type": "radio",
      "label": "Other",
      "value": 13,
      "subFields": [
        {
          "id": "question_2a",
          "type": "string",
          "condition": {
            "type": "BinaryExpression",
            "operator": "==",
            "left": {
              "type": "Identifier",
              "name": "question_2"
            },
            "right": {
              "type": "Literal",
              "value": 13
            }
          }
        }
      ]
    }
  ]
}
{
  "id": "question_2_b",
  "type": "radioGroup",
  "description": "Description3",
  "anwers": [
    {
      "type": "radio",
      "label": "Label - Radio 1",
      "value": 1
    },
    {
      "type": "radio",
      "label": "Label - Radio 2",
      "value": 2
    },
    {
      "type": "radio",
      "label": "Label - Radio 3",
      "value": 3
    }
  ]
}
{
  "id": "question_3",
  "type": "radioGroup",
  "description": "Description3",
  "anwers": [
    {
      "type": "radio",
      "label": "Yes",
      "value": 1
    },
    {
      "type": "radio",
      "label": "No",
      "value": 0
    }
  ]
}

我的问题是我不知道如何删除这些部分:

{
  "uiComponent": "SmallContent",
  "componentProps": {
    "text": "* If the answer to the above question is “Yes”, please contact the Support immediately."
  }
}

  "subFields": [
    {
      "id": "question_2a",
      "type": "string",
      "condition": {
        "type": "BinaryExpression",
        "operator": "==",
        "left": {
          "type": "Identifier",
          "name": "question_2"
        },
        "right": {
          "type": "Literal",
          "value": 13
        }
      }
    }
  ]

我只为 question_3 使用了这个 jq:

jq 玩 2

.subFields[] |  {id: .id, title: .title, description: .description, type: .type, subFields: .subFields} | select(has("id") and .id != null) | select(.id=="question_3") |  {id: .id, title: .title, description: .description, type: .type, questions: (.subFields[]|join(","))}

并产生了这个结果:

{
  "id": "question_3",
  "title": "Title3",
  "description": "Description3",
  "type": "radioGroup",
  "questions": "radio,Yes,1"
}
{
  "id": "question_3",
  "title": "Title3",
  "description": "Description3",
  "type": "radioGroup",
  "questions": "radio,No,0"
}

并且

jq 玩 3

.subFields[] |  {id: .id, title: .title, description: .description, type: .type, subFields: .subFields} | select(has("id") and .id != null) | select(.id=="question_3") |  [.id, .title, .description, .type, (.subFields[]|join(","))]

结果是:

[
  "question_3",
  "Title3",
  "Description3",
  "radioGroup",
  "radio,Yes,1",
  "radio,No,0"
]

你能帮我改进我创建的那些 JQ 模式以获得预期的结果吗?

提前致谢!

以下产生您的第一个替代方案:

.subFields[]
| select(.id?)
| { id, title, description, type} +
    (.subFields[] 
     | select(.type?) 
     | [.type,.label,.value] | join(",")
     |  { questions: .} )

注意两个select()过滤器。

此处明确指定了键名,以确保遵守您指定的顺序。

暂无
暂无

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

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