json 如何从JOLT转换数组的输出中为每个项删除一组“{}”,并在所有项周围添加一组?

2ul0zpep  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(78)

使用Nifi JoltTransformJSON处理器,对使用JOLT规范还很陌生,不知道如何将数组中每个项的一组{}移动到输出JSON中数组所有项周围的一组{}。请参阅下面的OUTPUT和NEEDED OUTPUT部分进行比较。我尝试通过阅读JOLT定义和一些示例来创建规范,这些示例让我非常接近。但这最后一部分已经踢我的屁股有一段时间了,任何提示都会很棒!
输入JSON:

{
  "AssetID": "1",
  "AssetNumber": "2",
  "AssetMaterial": "Cisco MDS 9706",
  "RackUnits": "9.0",
  "MaterialType": "Chassis",
  "AssetName": "Cisco-MDS-9706_1",
  "CustRID": "A001",
  "SerialNumber": "OU812",
  "Room": "ROOM5",
  "Datacenter": "DC69",
  "UMountingID": "86",
  "CabinetAssetID": "181",
  "CabinetName": "CAB666"
}

当前质量标准:

[
  {
    "operation": "shift",
    "spec": {
      "AssetID": "data[].6.value",
      "AssetNumber": "data[].7.value",
      "AssetMaterial": "data[].8.value",
      "AssetName": "data[].9.value",
      "CustRID": "data[].10.value",
      "SerialNumber": "data[].11.value",
      "Room": "data[].12.value",
      "Datacenter": "data[].13.value",
      "UMountingID": "data[].14.value",
      "CabinetAssetID": "data[].15.value",
      "CabinetName": "data[].16.value"
    }
  },
  {
    "operation": "default",
    "spec": {
      "to": "table1"
    }
  },
  {
    "operation": "default",
    "spec": {
      "fieldsToReturn": [6, 7, 8, 9, 10, 11, 12]
    }
  }
]

输出JSON:

{
  "data": [
    {
      "6": {
        "value": "1"
      }
    },
    {
      "7": {
        "value": "2"
      }
    },
    {
      "8": {
        "value": "Cisco MDS 9706"
      }
    },
    {
      "9": {
        "value": "Cisco-MDS-9706_1"
      }
    },
    {
      "10": {
        "value": "A001"
      }
    },
    {
      "11": {
        "value": "OU812"
      }
    },
    {
      "12": {
        "value": "ROOM5"
      }
    },
    {
      "13": {
        "value": "DC69"
      }
    },
    {
      "14": {
        "value": "86"
      }
    },
    {
      "15": {
        "value": "181"
      }
    },
    {
      "16": {
        "value": "CAB666"
      }
    }
  ],
  "to": "table1",
  "fieldsToReturn": [ 6, 7, 8, 9, 10, 11, 12 ]
}

要求/预期输出:

{
  "data" : [
    {
        "6" : {
          "value" : "1"
        }, 
        "7" : {
          "value" : "2"
        }, 
        "8" : {
          "value" : "Cisco MDS 9706"
        }, 
        "9" : {
          "value" : "Cisco-MDS-9706_1"
        }, 
        "10" : {
          "value" : "A001"
        }, 
        "11" : {
          "value" : "OU812"
        }, 
        "12" : {
          "value" : "ROOM5"
        }, 
        "13" : {
          "value" : "DC69"
        }, 
        "14" : {
          "value" : "86"
        }, 
        "15" : {
          "value" : "181"
        }, 
        "16" : {
          "value" : "CAB666"
        }
    }
   ],
  "to" : "table1",
  "fieldsToReturn" : [ 6, 7, 8, 9, 10, 11, 12 ]
}
jogvjijk

jogvjijk1#

您需要的似乎是顶层对象应该驻留在数组data的公共索引处,因此使用index 0(如**data[0].**)将这些对象组合为单个对象,如

[
  {
    "operation": "shift",
    "spec": {
      "AssetID": "data[0].6.value",
      "AssetNumber": "data[0].7.value",
      "AssetMaterial": "data[0].8.value",
       ...
       ...
      "#table1": "to"
    }
  },
  {
    "operation": "default",
    "spec": {
      "fieldsToReturn": [6, 7, 8, 9, 10, 11, 12]
    }
  },
  {
    "operation": "sort"
  }
]

顺便说一句,第一个default转换规范是多余的,不如使用

"#table1": "to"

shift转换规范内

相关问题