json 使用Jolt规范将字符串列表转换为对象

bfrts1fy  于 2023-01-22  发布在  其他
关注(0)|答案(1)|浏览(169)

需要将下面的JSON转换为所需的格式,这里SpecList是需要转换为对象列表的字符串列表。

    • 输入:**
{
  "Address": "ABC",
  "latitude": "xy.zx",
  "longitude": "xy.zm",
  "name": "name",
  "pincode": "123456",
  "registrationId": "xxcd",
  "Doctor": [
    {
      "Name": "ABC",
      "SpecList": [
        "A",
        "B",
        "C"
      ],
      "practitonerId": "z",
      "location": {
        "lat": "ab.cd",
        "lng": "ab.cd"
      }
    },
    {
      "Name": "def",
      "SpecList": [
        "D",
        "E"
      ],
      "practitonerId": "22",
      "location": {
        "lat": "ef.gh",
        "lng": "ef.gh"
      }
    }
  ]
}
    • 当前Jolt规范**,但未获得所需输出:
[
  {
    "operation": "shift",
    "spec": {
      "Address": "hospitalAddress",
      "latitude": "hospitalLatitude",
      "longitude": "hospitalLongitude",
      "name": "hospitalName",
      "pincode": "hospitalPincode",
      "registrationId": "hospitalRegistrationId",
      "Doctor": {
        "*": {
          "Name": "doctors[#2].doctorName",
          "practitonerId": "doctors[#2].doctorPractitionerId",
          "SpecList": {
            "*": {
              "@": "doctors[#1].doctorSpecializations.[].specializationName"
            }
          }
        }
      }
    }
  }
]
    • 预期产出:**
{
  "hospitalAddress" : "ABC",
  "hospitalLatitude" : "xy.zx",
  "hospitalLongitude" : "xy.zm",
  "hospitalName" : "name",
  "hospitalPincode" : "123456",
  "hospitalRegistrationId" : "xxcd",
  "doctors" : [ {
    "doctorName" : "abc",
    "doctorPractitionerId" : "z",
    "doctorSpecializations" : [ {
      "specializationName" : "A"
    }, {
      "specializationName" : "B"
    }, {
      "specializationName": "C"
    } ]
  }, {
    "doctorName" : "def",
    "doctorPractitionerId" : "22"
    "doctorSpecializations" : [ {
      "specializationName" : "D"
    }, {
      "specializationName" : "E"
    } ]
  }]
 }
cqoc49vn

cqoc49vn1#

可以使用此等级库:
正在获取&数组的索引:

[
  {
    "operation": "shift",
    "spec": {
      "Address": "hospitalAddress",
      "latitude": "hospitalLatitude",
      "longitude": "hospitalLongitude",
      "name": "hospitalName",
      "pincode": "hospitalPincode",
      "registrationId": "hospitalRegistrationId",
      "Doctor": {
        "*": {
          "Name": "doctors[&1].doctorName",
          "practitonerId": "doctors[&1].doctorPractitionerId",
          "SpecList": {
            "*": {
              "@": "doctors[&3].doctorSpecializations.[&1].specializationName"
            }
          }
        }
      }
    }
  }
]

如果您想使用#,您可以使用以下规格:

[
  {
    "operation": "shift",
    "spec": {
      "Address": "hospitalAddress",
      "latitude": "hospitalLatitude",
      "longitude": "hospitalLongitude",
      "name": "hospitalName",
      "pincode": "hospitalPincode",
      "registrationId": "hospitalRegistrationId",
      "Doctor": {
        "*": {
          "Name": "doctors[#2].doctorName",
          "practitonerId": "doctors[#2].doctorPractitionerId",
          "SpecList": {
            "*": {
              "@": "doctors[#4].doctorSpecializations.[#2].specializationName"
            }
          }
        }
      }
    }
  }
]
    • 注意:**您还可以将SpecList总结如下:
"SpecList": {
  "*": "doctors[#3].doctorSpecializations.[#1].specializationName"
}

相关问题