简体   繁体   English

使用 JOLT 转换重命名嵌套数组中的字段

[英]Rename fields in nested arrays using JOLT transformation

I want to rename fields in an array nested in an another array using JOLT transformation library.我想使用 JOLT 转换库重命名嵌套在另一个数组中的数组中的字段。 1. One field to rename is a top level field in an array 2. Two fields to rename are inside a nested array 1. 要重命名的一个字段是数组中的顶级字段 2. 要重命名的两个字段在嵌套数组中

I have tried using wildcards but they are not giving me expected output.我试过使用通配符,但它们没有给我预期的输出。 I am using JOLT 0.0.22 version.我正在使用 JOLT 0.0.22 版本。

Input JSON:输入 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"
                }
            ]
        }
    ]
}

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"
            }
          }
        }
      }
    }
  }
]

For comparison, this is the solution in JSLT .为了比较,这是JSLT 中的解决方案。

{
  "state" : [for (.state) . | {
    "locatedIn" : .location,
    "cities" : [for (.cities) {
      "cityname" : .name,
      "citypopulation" : .pop
    }],
    * : .
  }],
  * : .
}

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

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