json Jolt -在数组列表中插入新字段

fkaflof6  于 2023-11-20  发布在  其他
关注(0)|答案(2)|浏览(90)

我有问题与Jolt转换,我需要提取值/字段从数组列表和添加一个自定义字段无论是从数组我的目标提取或外部列表.但让我尝试通过代码解释更详细.
这是我的输入(在“data”中只有2个字段集,最初我在“data”中有200多个字段集):

{
  "result": {
    "id": "123456789",
    "year": 2023,
    "version": "0.1",
    "country": "UK",
    "modified": "2023-11-09T00:00:00",
    "data": [
      {
        "FieldNumber1": "CompanyNumber",
        "FieldNumber2": "0.1",
        "FieldNumber3": "123456789",
        "FieldNumber4": false,
        "FieldNumber5": false,
        "FieldNumber6": 1,
        "FieldNumber7": 0
      },
      {
        "FieldNumber1": "CompanyName",
        "FieldNumber2": "0.1",
        "FieldNumber3": "FunctionsAndStrings",
        "FieldNumber4": false,
        "FieldNumber5": false,
        "FieldNumber6": 1,
        "FieldNumber7": 0
      }
    ],
    "description": null,
    "downloadNumber": "aaaaa-bbbbb-11111-22-abcabc123"
  },
  "true": null,
  "false": null
}

字符串
我期待的结果是这样的:

[
  {
    "id": "123456789",
    "FieldNumber1": "CompanyNumber",
    "FieldNumber2": "0.1",
    "FieldNumber3": "123456789",
    "FieldNumber4": false,
    "FieldNumber5": false,
    "FieldNumber6": 1,
    "FieldNumber7": 0
  },
  {
    "id": "123456789",
    "FieldNumber1": "CompanyName",
    "FieldNumber2": "0.1",
    "FieldNumber3": "FunctionsAndStrings",
    "FieldNumber4": false,
    "FieldNumber5": false,
    "FieldNumber6": 1,
    "FieldNumber7": 0
  }
]


[
  {
    "CompanyNumber": "123456789",
    "FieldNumber1": "CompanyNumber",
    "FieldNumber2": "0.1",
    "FieldNumber3": "123456789",
    "FieldNumber4": false,
    "FieldNumber5": false,
    "FieldNumber6": 1,
    "FieldNumber7": 0
  },
  {
    "CompanyNumber": "123456789",
    "FieldNumber1": "CompanyName",
    "FieldNumber2": "0.1",
    "FieldNumber3": "FunctionsAndStrings",
    "FieldNumber4": false,
    "FieldNumber5": false,
    "FieldNumber6": 1,
    "FieldNumber7": 0
  }
]


我想只有一个列表从“数据”,但固定的“id”从“结果”,每个数据集之前。此外,在第一个数据集“FieldNumber 1”将始终是“CompanyNumber”,它将始终包含相同的值在“FieldNumber 3”作为“id”在“结果”。也许更容易计算它从“FieldNumber 3”。
我没有进一步移动比删除“结果”和提取只有“数据”列表与这个简单的震动规范:

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "data": ""
      }
    }
  }
]


我需要在表中存储第一个属性为“id”或“CompanyNumber”的每一组数据,这样我就可以将它与其他表连接起来。
非常感谢!

ar5n3qh5

ar5n3qh51#

您可以使用以下Jolt规格

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "data": {
          "*": {
            "@(2,id)": "[#4].&1.id",
            "*": "[#4].&1.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]

字符串

k7fdbhmy

k7fdbhmy2#

您需要的是循环遍历数据数组下的所有索引,同时在shift转换中从id的声明级别(例如,向上两级树)拾取id的值,例如

[
  {
    "operation": "shift",
    "spec": {
      "result": {
        "data": {
          "*": {
            "@2,id": "[#2].id", // @2, -> in order to go two levels up the tree and reach 
                                //        the level of "id"
            "*": "[#2].&"       // [#2]-> go two levels up the tree (eg. traverse : and {) 
                                //        in order to reach the level of indexes of 
                                //        the "data" array in arraywise manner           
          }
        }
      }
    }
  }
]

字符串
网站http://jolt-demo.appspot.com/上的 * 演示 * 是:


的数据
id键可以通过使用"@2,id" : "[#2].CompanyNumber"切换到CompanyNumber

相关问题