【问题标题】:Renaming fields in an array nested in an another array using JOLT transformation library使用 JOLT 转换库重命名嵌套在另一个数组中的数组中的字段
【发布时间】:2020-12-20 15:54:39
【问题描述】:

我刚开始学习颠簸变换。我发现了这种情况。该规范也已发布,但我试图理解规范,并与使用“&”的字段级别混淆。 从以下规格: 我对这种语法“name”感到困惑:“state[&3].cities[&1].cityname”。 为什么 "name": "state[&2].cities[&1].cityname" 没有给出任何输出?据我了解,城市名称的状态级别是&2。有人可以尝试解释为什么使用 &3。

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

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

【问题讨论】:

    标签: json wildcard transformation shift jolt


    【解决方案1】:
        {
        "operation": "shift",
        "spec": {
          "country": "country",
          "state": {
            "*": { // state array index                             <- level 3
              "stateName": "state[&1].stateName",
              "location":  "state[&1].location",
              "cities": {                                           <- level 2  
                "*": { // city array index                          <- level 1
                  "name": "state[&3].cities[&1].cityname",          <- level 0
                  "pop":  "state[&3].cities[&1].citypopualtion"
                }
              }
            }
          }
        }
      }  
    

    您实际上是在检索由“*”表示的索引。如果您使用 &2,您将获得城市和兄弟姐妹级别,但您不想访问这些属性。所以在转换过程中,它实际上最终会变成 state[state_index].cities[city_index].cityname

    【讨论】:

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