简体   繁体   English

如何使用 JOLT 转换将键从嵌套的 JSON object 向上移动

[英]How to move a key to up level from nested JSON object using JOLT Transformation

I want to do a JOLT Transformation and shift the key inside a nested JSON object to uplevel.我想进行 JOLT 转换并将嵌套 JSON object 内的密钥移动到上层。

Here is my input JSON :这是我的输入 JSON

[
  {
    "deviceNumber": "1287391731_city",
    "country": {
      "State": [
        {
          "Priority": "MEDIUM",
          "City": {
            "Town": [
              {
                "unit": "",
                "type": "",
                "value": "Mumbai",
                "key": "District1"
              },
              {
                "unit": "",
                "type": "",
                "value": "Delhi",
                "key": "District2"
              },
              {
                "unit": "",
                "type": "",
                "value": "2",
                "key": "DistrictCount"
              }
            ]
          },
          "description": "describe the subcategory"
        }
      ]
    },
    "location": "8.21311,76.018231"
  },
  {
    "deviceNumber": "1287391731_city",
    "country": {
      "State": [
        {
          "Priority": "MEDIUM",
          "City": {
            "Town": [
              {
                "unit": "",
                "type": "",
                "value": "Bangalore",
                "key": "District1"
              },
              {
                "unit": "",
                "type": "",
                "value": "Chennai",
                "key": "District2"
              },
              {
                "unit": "",
                "type": "",
                "value": "2",
                "key": "DistrictCount"
              }
            ]
          },
          "description": "describe the subcategory"
        }
      ]
    },
    "location": "8.21311,76.018231"
  },
  {
    "deviceNumber": "1287391731_city",
    "country": {
      "State": [
        {
          "Priority": "MEDIUM",
          "City": {
            "Town": [
              {
                "unit": "",
                "type": "",
                "value": "Ahmedabad",
                "key": "District1"
              },
              {
                "unit": "",
                "type": "",
                "value": "Kolkata",
                "key": "District2"
              },
              {
                "unit": "",
                "type": "",
                "value": "2",
                "key": "DistrictCount"
              }
            ]
          },
          "description": "describe the subcategory"
        }
      ]
    },
    "location": "8.21311,76.018231"
  }
]

Here is the expected Output :这是预期的 Output

[
  {
    "@context": "https://my_context.in",
    "type": [
      "CITYVIEW"
    ],
    "deviceNumber": "1287391731_city",
    "DistrictCount": 2,
    "location": "8.21311,76.018231",
    "District1": {
      "NormalValue": "Delhi"
    },
    "District2": {
      "NormalValue": "Mumbai"
    }
  },
  {
    "@context": "https://my_context.in",
    "type": [
      "CITYVIEW"
    ],
    "deviceNumber": "1287391731_city",
    "DistrictCount": 2,
    "location": "8.21311,76.018231",
    "District1": {
      "NormalValue": "Bangalore"
    },
    "District2": {
      "NormalValue": "Chennai"
    }
  },
  {
    "@context": "https://my_context.in",
    "type": [
      "CITYVIEW"
    ],
    "deviceNumber": "1287391731_city",
    "DistrictCount": 2,
    "location": "8.21311,76.018231",
    "District1": {
      "NormalValue": "Ahmedabad"
    },
    "District2": {
      "NormalValue": "Kolkata"
    }
  }
]

Here districtCount is shifted uplevel and the value is shown as an integer. Type and context are default values and be created using default operation.这里districtCount向上移动,值显示为 integer。类型和上下文是默认值,使用默认操作创建。 District1 and District2 can be shifted using the following spec:可以使用以下规范移动 District1 和 District2:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "country": {
          "*": {
            "*": {
              "*": {
                "*": ""
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": "@(1,key).NormalValue"
      }
    }
  }
]

This is the output I am getting:这是我得到的 output:

{
  "District1": {
    "NormalValue": "Mumbai"
  },
  "District2": {
    "NormalValue": "Delhi"
  },
  "DistrictCount": {
    "NormalValue": "2"
  }
}

Could you please help me in writing the spec for JOLT transformation to get the expected output?您能否帮我编写 JOLT 转换规范以获得预期的 output?

You can use this spec:您可以使用此规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "#https:\//my_context.in": "[&1].\\@context",
        "*": "[&1].&",
        "type": "[&1].&[]",
        "country": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "*": {
                    "*": {
                      "DistrictCount": {
                        "@(2,value)": "[&9].@2"
                      },
                      "District*": {
                        "@(2,value)": "[&9].@2.NormalValue"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "DistrictCount": "=toInteger"
      }
    }
  }
]

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

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