Rename fields in nested arrays using JOLT transformation

元气小坏坏 提交于 2020-05-08 05:35:50

问题


I want to rename fields in an array nested in an another array using JOLT transformation library. 1. One field to rename is a top level field in an array 2. Two fields to rename are inside a nested array

I have tried using wildcards but they are not giving me expected output. I am using JOLT 0.0.22 version.

Input JSON:

{
    "country": "usa",
    "state": [
        {
            "stateName": "TX",
            "location": "south",
            "cities": [
                {
                    "name": "Austin",
                    "pop": "1M"
                },
                {
                    "name": "Dallas",
                    "pop": "2M"
                }
            ]
        },
        {
            "stateName": "CA",
            "location": "west",
            "cities": [
                {
                    "name": "SanFran",
                    "pop": "3M"
                },
                {
                    "name": "LosAngeles",
                    "pop": "4M"
                }
            ]
        }
    ]
}

Expected Output :

{
    "country": "usa",
    "state": [
        {
            "stateName": "TX",
            "locatedIn": "south",  // name change here
            "cities": [
                {
                    "cityname": "Austin",  // name change here
                    "citypopulation": "1M" // name change here
                },
                {
                    "cityname": "Dallas",
                    "citypopulation": "2M"
                }
            ]
        },
        {
            "stateName": "CA",
            "locatedIn": "west",
            "cities": [
                {
                    "cityname": "SanFran",
                    "pop": "3M"
                },
                {
                    "cityname": "LosAngeles",
                    "citypopulation": "4M"
                }
            ]
        }
    ]
}

回答1:


Spec

[
  {
    "operation": "shift",
    "spec": {
      "country": "country",
      "state": {
        "*": { // state array index
          "stateName": "state[&1].stateName",
          "location":  "state[&1].location",
          "cities": {
            "*": { // city array index
              "name": "state[&3].cities[&1].cityname",
              "pop":  "state[&3].cities[&1].citypopualtion"
            }
          }
        }
      }
    }
  }
]


来源:https://stackoverflow.com/questions/39585360/rename-fields-in-nested-arrays-using-jolt-transformation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!