-1

I have input json

[
  {
    "nm": "BRANCHID",
    "vl": "1"
  },
  {
    "nm": "TELLERID",
    "vl": "9903"
  },
  {
    "nm": "mobile",
    "vl": "9903234565"
  }
]

I need to check nm equal to BRANCHID or TELLERID then would set V1 value accordingly and output:

{"BRANCHID":"1"},
{"TELLERID",:"9903"}
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55

2 Answers2

2

Filter by the items you need and then construct a new object combining the nm and vl attributes:

%dw 2.0
output application/json
---
payload 
   filter ((item) -> item.nm == 'BRANCHID' or item.nm == "TELLERID") 
   map ((item) -> {(item.nm) : item.vl})
afelisatti
  • 2,770
  • 1
  • 13
  • 21
0

Sort the data into a new object. There's probably a more sophisticated way to do this, but this should work.

let newObj = {};
json.forEach(sort);

function sort(item, index){
    newObj[ json[index].nm ] = json[index].vl;
}

https://jsfiddle.net/r75haec8/

thingEvery
  • 3,368
  • 1
  • 19
  • 25