如何展平嵌套的json

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

我想展平嵌套的输入 JSON。 JSON 包含具有“n”个产品属性的产品。附加到产品的是“n”个项目,每个项目可以具有“n”个项目属性。我的目标是将所有信息带到根级别。我可以对顶层执行此操作,但不能对嵌套数组执行此操作。也许我需要分多个步骤执行此操作,但我不确定如何执行。 这是我输入的 Json:

{
  "product": {
    "attributes": [
      {
        "values": [
          {
            "unit_of_measure": null,
            "language": null,
            "value": "changable"
          },
          {
            "unit_of_measure": null,
            "language": null,
            "value": "washable"
          }
        ],
        "id": "cleaning",
        "kind": "option"
      },
      {
        "values": [
          {
            "unit_of_measure": null,
            "language": null,
            "value": "myTest"
          }
        ],
        "id": "how_to_purchase",
        "kind": "String"
      }
    ],
    "title": "myTitle",
    "category": {
      "id": "47114712"
    },
    "items": [
      {
        "item_number": "4711",
        "attributes": [
          {
            "values": [
              {
                "unit_of_measure": null,
                "language": null,
                "value": "2024-01-25T09:11:49"
              }
            ],
            "id": "timestamp",
            "kind": "String"
          },
          {
            "values": [
              {
                "unit_of_measure": "kg",
                "language": null,
                "value": 0.47
              }
            ],
            "id": "weight",
            "kind": "float"
          },
          {
            "values": [
              {
                "unit_of_measure": null,
                "language": null,
                "value": "orange"
              },
              {
                "unit_of_measure": null,
                "language": null,
                "value": "black"
              }
            ],
            "id": "colour",
            "kind": "option"
          }
        ]
      },
      {
        "item_number": "4712",
        "attributes": [
          {
            "values": [
              {
                "unit_of_measure": null,
                "language": null,
                "value": "2024-01-25T09:11:49"
              }
            ],
            "id": "timestamp",
            "kind": "String"
          },
          {
            "values": [
              {
                "unit_of_measure": null,
                "language": null,
                "value": true
              }
            ],
            "id": "valid",
            "kind": "boolean"
          }
        ]
      }
    ],
    "product_number": "myProduct123"
  }
}

这是我想要的扁平化输出(考虑到一个属性可以有多个值)

{
    "p-myProduct123-cleaning-0-unit_of_measure": null,
    "p-myProduct123-cleaning-0-language": null,
    "p-myProduct123-cleaning-0-value": "changable",
    "p-myProduct123-cleaning-1-unit_of_measure": null,
    "p-myProduct123-cleaning-1-language": null,
    "p-myProduct123-cleaning-1-value": "washable",
    "p-myProduct123-cleaning-id": "cleaning",
    "p-myProduct123-cleaning-kind": "option",
    "p-myProduct123-how_to_purchase-0-unit_of_measure": null,
    "p-myProduct123-how_to_purchase-0-language": null,
    "p-myProduct123-how_to_purchase-0-value": "myTest",
    "p-myProduct123-how_to_purchase-id": "how_to_purchase",
    "p-myProduct123-how_to_purchase-kind": "String",
    "p-myProduct123-title": "myTitle",
    "p-myProduct123-category": "47114712",
    "i-4711-timestamp-0-unit_of_measure": null,
    "i-4711-timestamp-0-language": null,
    "i-4711-timestamp-0-value": "2024-01-25T09:11:49",
    "i-4711-timestamp-id": "timestamp",
    "i-4711-timestamp-kind": "String",
    "i-4711-weight-0-unit_of_measure": "kg",
    "i-4711-weight-0-language": null,
    "i-4711-weight-0-value": 0.47,
    "i-4711-weight-id": "weight",
    "i-4711-weight-kind": "float",
    "i-4711-colour-0-unit_of_measure": null,
    "i-4711-colour-0-language": null,
    "i-4711-colour-0-value": "orange",
    "i-4711-colour-1-unit_of_measure": null,
    "i-4711-colour-1-language": null,
    "i-4711-colour-1-value": "black",
    "i-4711-colour-id": "colour",
    "i-4711-colour-kind": "option",
    "i-4712-timestamp-0-unit_of_measure": null,
    "i-4712-timestamp-0-language": null,
    "i-4712-timestamp-0-value": "2024-01-25T09:11:49",
    "i-4712-timestamp-id": "timestamp",
    "i-4712-timestamp-kind": "String",
    "i-4712-valid-0-unit_of_measure": null,
    "i-4712-valid-0-language": null,
    "i-4712-valid-0-value": true,
    "i-4712-valid-id": "valid",
    "i-4712-valid-kind": "boolean"
}

任何帮助将不胜感激

直到知道,我只能在更高的水平上平整田野。

[
  {
    "operation": "shift",
    "spec": {
      "product": {
        "product_number": "@(1,product_number).product_number",
        "title": "@(1,product_number).title",
        "items": "@(1,product_number).items",
        "category": {
          "id": "@(2,product_number).category"
        },
        "attributes": "@(1,product_number).product_attributes"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "p-&1-&"
      }
    }
  }
]
json flatten jolt
1个回答
0
投票

JSON 主要有两个主要分支

attributes
items
嵌套在对象
product
中,您想分别为它们起别名
p
i

考虑到这些事实,您可以使用以下转换,其中 & 符号的标识符值后缀(例如 &1、&2、&3 等)由它们在树中的深度设置:

[
  {
    "operation": "shift",
    "spec": {
      "product": {
        "attributes": {
          "*": {
            "values": {
              "*": {
                "*": {
                  "@": "p.@(6,product_number).@(4,id).&2-&"
                }
              }
            },
            "*": "p.@(3,product_number).@(1,id).&1-&"
          }
        },
        "items": {
          "*": {
            "attributes": {
              "*": {
                "values": {
                  "*": {
                    "@": "i.@(5,item_number).@(3,id).&1"
                  }
                },
                "*": "i.@(3,item_number).@(1,id).&1-&"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "p": {
        "*": {
          "*": {
            "*": "&3-&2-&1-&"
          }
        }
      },
      "i": {
        "*": {
          "*": {
            "*": {
              "*": "&4-&3-&2-&1-&"
            }
          }
        }
      }
    }
  }
]

网站上的 演示 https://jolt-demo.appspot.com/ 是:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.