Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to JSON transformer

I got a scenario.

Required input and output are JSON.

// Input {   "OldObject": {     "Time": 1351160457922,     "Name": "OName",     "quantity": 100,     "price": 10   } }   // Output {   "NewObject": {     "Time": 1351160457922,     "Title": "OName",     "quantity": 100   } } 

I need some transformation code or preferably xslt type language to transform json from one format to another. This transformer also need to be fast as transformation will be done on the fly.

Edit
I don't have the definition of the INPUT object received and it might change at run time. but I can use class for OUTPUT object if needed. I have tried to do this as json -> xml -> xslt -> xml -> json, but approximately 1000 objects are received per second at this end and this process might incur overhead.
I can not also use JavaScript as myApp is simple windows based java application and using JavaScript might cause overhead.

like image 866
Chaturvedi Dewashish Avatar asked Oct 25 '12 12:10

Chaturvedi Dewashish


People also ask

What is jolt transform?

Jolt is an open-source JSON to JSON transformation library written in Java. Jolt's main features are: Provides a set of transformations to deal with the JSON-to-JSON conversion. Transforms the structure of the JSON data, not data manipulation. Uses a JSON file for the specification.

How do I format a JSON file?

Formatting# You can format your JSON document using Ctrl+Shift+I or Format Document from the context menu.


2 Answers

Try JOLT. It is a JSON to JSON transformation library written in Java. It was created on a project that was transforming lot of JSON from an ElasticSearch "backend" to a frontend api.

For the JSON transform you have listed in your problem, the Jolt "shift" spec would be :

// Jolt "shift" spec {     "OldObject": {         "Time": "NewObject.Time",            "Name": "NewObject.Title", // if the input has "OldObject.Name", copy it's value                                    // to "NewObject.Title         "quantity": "NewObject.quantity"     } } 
like image 80
Milo S Avatar answered Sep 21 '22 08:09

Milo S


You can do this transformation with JSON patch.

Example with jsonpatch-js:

var transformations = [   { move: '/OldObject', to: '/NewObject' },   { remove: '/NewObject/price' },   { move: '/NewObject/Name', to: '/NewObject/Title' } ];  var oldObject = { "OldObject": { "Time": 1351160457922, "Name": "OName", "quantity": 100, "price": 10 } };  jsonpatch.apply(oldObject, transformations); 

I did not test the provided, but should work like that.

There are Java implementations for JSON patch:

like image 39
KARASZI István Avatar answered Sep 18 '22 08:09

KARASZI István