最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

json - JOLT value to key and set value from another key.value - Stack Overflow

programmeradmin0浏览0评论

I need to transform multiple keys and their values to new JSON

source JSON

{
  "attributes": [
    {
      "key": "network",
      "value": {
        "stringValue": "4G"
      }
    },
    {
      "key": "device_id",
      "value": {
        "stringValue": "1234567890"
      }
    }
  ]
}

Expected output:

{
  "network": "4G",
  "device_id":"1234567890"
}

Thanks in advance

I need to transform multiple keys and their values to new JSON

source JSON

{
  "attributes": [
    {
      "key": "network",
      "value": {
        "stringValue": "4G"
      }
    },
    {
      "key": "device_id",
      "value": {
        "stringValue": "1234567890"
      }
    }
  ]
}

Expected output:

{
  "network": "4G",
  "device_id":"1234567890"
}

Thanks in advance

Share Improve this question edited Jan 30 at 8:39 Barbaros Özhan 65.4k11 gold badges36 silver badges61 bronze badges asked Jan 30 at 8:31 Будён Михайлович СемённыйБудён Михайлович Семённый 312 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

You can use a shift transformation that matches those keys :

[
  {
    "operation": "shift",
    "spec": {
      "*": {//stands for the level of the "attributes" array
        "*": {// for the indexes of the array
          "@value.stringValue": "@key"
        }
      }
    }
  }
]

the demo on the site Jolt Transform Demo Using v0.1.1 is :

Edit(based on the comment) : If only needed to bring the device_id, they you could use a conditional logic by the following spec :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "key": {
            "device_id": {
              "@2,value.stringValue": "&1"
            }
          }
        }
      }
    }
  }
]
发布评论

评论列表(0)

  1. 暂无评论