首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为嵌套数组编写JOLT规范

如何为嵌套数组编写JOLT规范
EN

Stack Overflow用户
提问于 2022-08-19 10:52:45
回答 1查看 46关注 0票数 1

我正在尝试使用using来转换JSON。这个JSON由嵌套数组组成,我无法正确地转换它。有人能帮忙吗。谢谢。

代码语言:javascript
复制
{
  "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"
          }
        }
      ]
    }
  ]
}

这是我写的规范。

代码语言:javascript
复制
[
  {
    "operation": "shift",
    "spec": {
      "root": {
        "*": {
          "id": "[&1].userid",
          "password": "[&1].pwd",
          "devices": {
            "*": {
              "details": {
                "deviceType": "[&2].deviceCategory",
                "deviceId": "[&2].deviceUniqueValue"
              }
            }
          }
        }
      }
    }
  }
]

我要寻找的预期JSON是:

代码语言:javascript
复制
[
  {
    "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"
        }
      }
    ]
  }
]

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

代码语言:javascript
复制
[ 
 {
   "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"
 } 
]

我想不出是怎么回事。有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-19 11:37:08

首先可以深入研究最内部的对象,同时通过id值通过shift转换对子对象进行分区,例如

代码语言:javascript
复制
[
  {
    "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": ""
            }
          }
        }
      }
    }
  }
]
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73415651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档