【问题标题】:How to write JOLT Spec for nested arrays如何为嵌套数组编写 JOLT 规范
【发布时间】:2022-12-22 02:40:25
【问题描述】:

我正在尝试使用 JOLT 转换 JSON。此 JSON 由嵌套数组组成,我无法正确转换它。有人可以帮忙吗?谢谢。

{
  "root": [
    {
      "id": "1234",
      "password": "key1234",
      "devices": [
        {
          "details": {
            "deviceType": "tv-iot",
            "deviceId": "tv-iot-111"
          }
        },
        {
          "details": {
            "deviceType": "machine-iot",
            "deviceId": "machine-iot-999"
          }
        }
      ]
    },
    {
      "id": "6789",
      "password": "key6789",
      "devices": [
        {
          "details": {
            "deviceType": "phone-iot",
            "deviceId": "phone-iot-111"
          }
        },
        {
          "details": {
            "deviceType": "mobile-iot",
            "deviceId": "mobile-iot-999"
          }
        }
      ]
    }
  ]
}

这是我写的规范。

[
  {
    "operation": "shift",
    "spec": {
      "root": {
        "*": {
          "id": "[&1].userid",
          "password": "[&1].pwd",
          "devices": {
            "*": {
              "details": {
                "deviceType": "[&2].deviceCategory",
                "deviceId": "[&2].deviceUniqueValue"
              }
            }
          }
        }
      }
    }
  }
]

我正在寻找的预期 JSON 是:

[
  {
    "userid": "1234",
    "pwd": "key1234",
    "devices": [
      {
        "details": {
          "deviceCategory": "tv-iot",
          "deviceUniqueValue": "tv-iot-111"
        }
      },
      {
        "details": {
          "deviceCategory": "machine-iot",
          "deviceUniqueValue": "machine-iot-999"
        }
      }
    ]
  },
  {
    "userid": "6789",
    "pwd": "key6789",
    "devices": [
      {
        "details": {
          "deviceCategory": "phone-iot",
          "deviceUniqueValue": "phone-iot-111"
        }
      },
      {
        "details": {
          "deviceCategory": "mobile-iot",
          "deviceUniqueValue": "mobile-iot-999"
        }
      }
    ]
  }
]

但是,我得到了这个错误的输出。不知何故,我的嵌套对象正在转换为列表。

[ 
 {
   "userid" : "1234",
   "pwd" : "key1234",
   "deviceCategory" : [ "tv-iot", "phone-iot" ],
   "deviceUniqueValue" : [ "tv-iot-111", "phone-iot-111" ]
 }, 
 {
   "deviceCategory" : [ "machine-iot", "mobile-iot" ],
   "deviceUniqueValue" : [ "machine-iot-999", "mobile-iot-999" ],
   "userid" : "6789",
   "pwd" : "key6789"
 } 
]

我无法弄清楚出了什么问题。有人可以帮忙吗?

【问题讨论】:

    标签: json multidimensional-array apache-nifi transformation jolt


    【解决方案1】:

    您可以首先深入研究最里面的对象,同时按以下方式划分子对象ID通过一个值转移转换如

    [
      {
        "operation": "shift",
        "spec": {
          "root": {
            "*": {
              "devices": {
                "*": {
                  "details": {
                    "*": {
                      "@(4,id)": "@(5,id).userid",
                      "@(4,password)": "@(5,id).pwd",
                      "@": "@(5,id).devicedetails[&3].&2.&1"
                    }
                  }
                }
              }
            }
          }
        }
      },
      {
        // get rid of top level object names
        "operation": "shift",
        "spec": {
          "*": ""
        }
      },
      {
        // get rid of repeating components of each arrays respectively
        "operation": "cardinality",
        "spec": {
          "*": {
            "us*": "ONE",
            "pwd": "ONE"
          }
        }
      },
      {
        // determine new key names for attributes respectively
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "deviceCategory": "=(@(1,deviceType))",
                  "deviceUniqueValue": "=(@(1,deviceId))"
                }
              }
            }
          }
        }
      },
      {
        // get rid of extra elements generated
        "operation": "remove",
        "spec": {
          "*": {
            "*": {
              "*": {
                "*": {
                  "deviceType": "",
                  "deviceId": ""
                }
              }
            }
          }
        }
      }
    ]
    

    【讨论】:

    • 非常感谢 Barbaros 及时而完美的回应。标记为已接受。
    【解决方案2】:

    更新:能够定义一个更短的规范来完成工作!

    [
      {
        "operation": "shift",
        "spec": {
          "root": {
            "*": {
              "id": "[&1].userId",
              "password": "[&1].pwd",
              "*": "[&1].&"
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "devices": {
              "*": {
                "details": {
                  "deviceType": "[&4].&3.[&2].&1.deviceCategory",
                  "deviceId": "[&4].&3.[&2].&1.deviceUniqueVal"
                }
              }
            },
            "*": "[&1].&"
          }
        }
      }
    ]
    
    

    【讨论】:

      猜你喜欢
      • 2023-04-10
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多