简体   繁体   中英

JSON to JSON transformation using JOLT

input

{
"ances": [
    {
        "id": 1,
        "level": "Building",
        "name": " Metro Campus Outdoor"
    },
    {
        "id": 2,
        "level": "Campus",
        "name": " Metro Campus"
    }
]

}

expected output:

{
  "result": [
    {
      "Building_id": 1,
      "Building": "my building for Outdoor"
    },
    {
      "Building_id": 2,
      "Campus": "Man Metro Campus"
    }
  ]
}


i want to change the name key with the value of the level tag.

Spec

[
  {
    "operation": "shift",
    "spec": {
      "ances": {
        "*": {
          "id": "result[&1].Building_id",
          "level": {
            "Building": {
              // if we matched all the way down here
              // then go back up and grab the "name" 
              // and write it's value to Building in
              // the result
              "@(2,name)": "result[&3].Building"
            },
            "Campus": {
              "@(2,name)": "result[&3].Campus"
            }
          }
        }
      }
    }
  }
]

Updated Spec per comment, where Campus and Building should not be hard coded

[
  {
    "operation": "shift",
    "spec": {
      "ances": {
        "*": {
          "id": "result[&1].Building_id",
          "level": {
            "*": {
              // if we matched all the way down here
              // then go back up and grab the "name" 
              // and write it's value to 
              // the result array, using the value of 
              //  level as the key
              "@(2,name)": "result[&3].&1"
            }
          }
        }
      }
    }
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM