【问题标题】:Rename fields in nested arrays using JOLT transformation使用 JOLT 转换重命名嵌套数组中的字段
【发布时间】:2016-09-20 03:42:45
【问题描述】:

我想使用 JOLT 转换库重命名嵌套在另一个数组中的数组中的字段。 1.要重命名的字段是数组中的顶级字段 2.要重命名的两个字段在一个嵌套数组中

我尝试使用通配符,但它们没有给我预期的输出。我使用的是 JOLT 0.0.22 版本。

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

预期输出:

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

【问题讨论】:

    标签: json jolt


    【解决方案1】:

    规格

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

    【讨论】:

    【解决方案2】:

    为了比较,这是JSLT中的解决方案。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-15
      相关资源
      最近更新 更多