0

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.
Milo S
  • 4,466
  • 1
  • 19
  • 22
Shilpi
  • 109
  • 3
  • 12

1 Answers1

0

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"
            }
          }
        }
      }
    }
  }
]
Milo S
  • 4,466
  • 1
  • 19
  • 22
  • Hi Milo, Thanks for your help. but i dont want to hard code building or Campus(it could be anything), i want value of level field (whatever it is) will be the key and value of that key is the value of name field. please suggest if there is any solution. – Shilpi Jul 27 '17 at 05:13
  • Hi Milo, one more help please : if i am updating my input like { "devid" = " 1000" "ances": [ { "id": 1, "level": "Building", "name": " Metro Campus Outdoor" }, { "id": 2, "level": "Campus", "name": " Metro Campus" } ] – Shilpi Sep 06 '17 at 09:37
  • Hi Milo, one more help please : if i am updating my input like { "devid" = " 1000" "ances": [ { "id": 1, "level": "Building", "name": " Metro Campus Outdoor" }, { "id": 2, "level": "Campus", "name": " Metro Campus" }] in spec when i am adding "devid": "result.devid" before "ances" it is just pinting devid only but my expected output { "result":{ "devid":"1000" [ { "Building_id": 1, "Building": "my building for Outdoor" }, { "Building_id": 2, "Campus": "Man Metro Campus" } ]} } – Shilpi Sep 06 '17 at 09:47
  • i have json { "deveui": "1000000000000000", "vars": [ { "nvoCfgNo": "255" }, { "nvoLampStatus": { "lampStatus": "unknown(0xff)", "lampLevel": "255" } }, { "nvoVolt": "255" }, {"gps": {"lat": "12.934638","lon": "77.693950" } }, {"status": "up"}, {"last_status_change": 1503452208 } ] } – Shilpi Sep 11 '17 at 10:28