json 如何在Jolt规范中将Name和nameId存储在Map中?

rfbsl7qr  于 2023-05-02  发布在  其他
关注(0)|答案(1)|浏览(134)

我想将***Name***和***nameId***一起存储在map中或数组中的任何 key:value 形式中,以便稍后在executeScript中使用。
我的条件是,如果名称匹配传入的数据,那么reasonId需要存储或访问。因此,我需要以一种稍后可以轻松使用的方式存储此Name和nameId。

[
  {
    "Name": "Ivor",
    "Specialty": [
      {
        "nameId": "554",
        "specialtyId": "20"
      }
    ]
  },
  {
    "Name": "Philip",
    "Specialty": [
      {
        "nameId": "524",
        "specialtyId": "70"
      }
    ]
  },
  {
    "Name": "Alex",
    "Specialty": [
      {
        "nameId": "594",
        "specialtyId": "90"
      }
    ]
  },
{
    "Name": "Adam",
    "Specialty": [
      {
        "nameId": "504",
        "specialtyId": "10"
      }
    ]
  }]

预期输出json为:

{
  "Name": {
    "Ivor": [
      "554"
    ],
    "Philip": [
      "524"
    ],
    "Alex": [
      "594"
    ],
    "Adam": [
      "504"
    ]
  }
}
bweufnob

bweufnob1#

您可以在**JoltTransformJSON处理器中使用这样的shift**转换规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@Specialty[0].nameId": "Name.@1,Name[]" 
                       // the outermost wrapper is Name 
                       // the value of the Name is the next level wrapper through use of .@1,Name
                       // the innermost "nameId" : <itsvalue> match by .nameId : .nameId
      }
    }
  }
]

或以下一个作为另一替代:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Specialty": {
          "*": {
            "nameId": "Name.@3,Name[]" // need to traverse one `:` + two `{`  = three levels in order to reach the level of the "Name"
          }
        }
      }
    }
  }
]

相关问题