8

The below post led me to evaluate using jasonpatch for json to json transformation:

JSON to JSON transformer

The project can be found here:

https://github.com/bruth/jsonpatch-js

I am currently trying to change the name of all elements in an array and am not seeing how this is possible. My current attempt is:

var transformations = [
{ op: 'move', from:'/hits/1/_id', path: '/hits/1/pizza'}
];

This swaps out the first element but how do I do a "*" card type operation? Something like:

var transformations = [
{ op: 'move', from:'/hits/*/_id', path: '/hits/*/pizza'}
];

I could see possibly calling the transformation N times for each element but that seems like a hack.

Community
  • 1
  • 1
Ryan R.
  • 2,298
  • 5
  • 24
  • 46

1 Answers1

3

Ended up using an approach where I wrapped the call to apply in a loop:

 for(i=0;i<json.hits.length;i++) {
    var transformations = [{ op: 'move', from:'/hits/'+i+'/_id', path:'/hits/'+i+'/pizza'}];
    var result = jsonpatch.apply(json,transformations);             
 }

Maybe jsonpatch could use a wildcard feature?

Ryan R.
  • 2,298
  • 5
  • 24
  • 46