Categories
Projects Technology Web

Easily Transform JSON with json2json

I have been working on an auto-complete web service that searches Amazon’s Product Advertising API.  I built it in Node.js and using the APAC package made it really easy to query the API. The only thing that was extremely impractical was the JSON data returned by APAC.

Since Amazon’s API only returns data as XML, APAC uses xml2json to convert the XML to JSON. Unfortunately the resulting JSON is quite ugly. I wanted to be able to choose the data I needed and copy it to a new, clean JSON format. My solution was to create json2json.

json2json lets you create a template that describes how to transform the original JSON to a new structure. I wrote the Node.js package and example template in CoffeeScript because it has a much cleaner and simpler syntax than JavaScript. However, it is extremely simple to convert to JavaScript (click on “Try CoffeeScript”) and can easily be modified for use in a browser. Check out the (crude) documentation and example files and let me know what you think.

9 replies on “Easily Transform JSON with json2json”

Hi, this looks great and I’d really like to use this in a browser instead of a Node environment. Is there any chance you could provide a version that runs in a browser, or provide guidance on how to modify it? I would we willing to be your tester. 🙂

Hi Aaron,

I updated the json2json code on GitHub to handle running in the browser. There’s a little blurb at the bottom of the README that shows what files you should include. Let me know if it works. 🙂

~Joel

Joel,

Is JSON 2 JSON under active development? This looks like something that I could use. I started following CoffeeScript recently. It looks like that language is under active development. I have been playing around with JSON 2 JSON, both as something useful in itself, and also as an application and example of the use of CoffeeScript.

Hi,
How do I use this in Node JS. I do not see any tutorial on this. Like doing a require and the APIs that can be used to transform using the template. Please help.

Being a newbie I managed to install the module in my node.js but I fail when it comes to actually transforming your example files.
My transformed result is always Null.

Maybe you can help me out what is wrong with my script?
I’ve added lots of console.logs and everything looks ok – until the actual tranformation.


require('coffee-script/register');
var json2json = require('json2json');
var fs = require('fs');
var input;
var outputfile;
var output;
var template;
input=fs.readFileSync(process.argv[2],{encoding:"UTF-8"});
console.log(input);
template = fs.readFileSync(process.argv[3],{encoding:"UTF-8"});
console.log(template);
outputfile=process.argv[4];
console.log(outputfile);
output= new json2json.ObjectTemplate(template).transform(input) ;
console.log(output);
fs.writeFileSync(outputfile, output,{encoding:"UTF-8"});
console.log("Done!")

How can we pass default value into “json2json-transform”.i can’t pass default value directly into template file.my code is below.

var transform = require(‘jsonpath-object-transform’);
var express = require(‘express’);
var app = express();

var data = {
‘name’ : ‘ritesh’
};

var template = {
‘status’ : ‘sucess’,
‘name’ : ‘$.name’
}
app.get(‘/’, function(req, res){

var result = transform(data, template);
res.json(result);

//res.sendFile(path.join(__dirname, ‘views/index.html’));
});

app.listen(5000, function () {
console.log(‘Web Console @ 127.0.0.1: 5000’);
});

Leave a Reply