在json中按id查找元素

时间:2018-02-12 19:13:27

标签: json transformation jolt

需要根据元素值进行转换 input.json

{
  "Result": {
    "owner": {
      "name": "test  user"
    },
    "Components": [
      {
        "id": "123-456-789"
      }
    ],
    "123-456-789": {
      "temp": {
        "emip": "abc",
        "teto": "123"
      }
    }
  }
}

变换json

[
  {
    "operation": "shift",
    "spec": {
      "Result": {
        "Components": {
          "*": {
            "id": "compId"
          }
        },
        "compId": {
          "@": "component"
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "compId": null
    }
  }
]

预计输出

 "123-456-789": {
      "temp": {
        "emip": "abc",
        "teto": "123"
      }
    }

但结果答案如下,当我用123-456-789硬编码值时,我得到了值,但我需要动态获取值。

{
  "compId" : "123-456-789"
}

1 个答案:

答案 0 :(得分:0)

规格

[
  {
    "operation": "shift",
    "spec": {
      "Result": {
        "Components": {
          "*": { // components array index
            "id": {
              "*": { // match any value of id
                // go back up the tree 5 levels, and then 
                //  lookup a key based on the value of the "id"
                //  and write that to the output at at that same 
                //  key
                "@(4,&)": "&1"
              }
            }
          }
        }
      }
    }
  }
]
相关问题