Wednesday, 4 September 2013

Converting JSON to have a new top-level wrapper

Converting JSON to have a new top-level wrapper

I suspect I am using the wrong terminology, so please bear with me.
I have been using mustache.js very effectively to work with the JSON
output of an API. I am working with a sibling API that has outlook that
looks mostly similar, excepting that there is no top-level name. I am
trying to figure out how to adjust the new JSON output into the other
format, such that I can continue using my mustache.js approach (this seems
like the easier of the options, the other being using something other than
mustache.js, which I am seeking to avoid).
I can use mustache.js to iterate very easily over the following JSON body
using the top-level name 'records':
{"records":
[{"dt_created":"08/23/2013 04:49:13 PM","created_by":"x,
x","dt_updated":"08/23/2013 04:49:13 PM","updated_by":"x,
x","field_60374": ["Project 67"],"field_60331":["Ability to adjust the
number of lines dynamically based on the mood of the person 3 rows down,
4th cube, 11th building. A breakthrough in mind-control data
processing."],"field_60333":
[{"id":"x","first":"x","last":"x"}],"field_60394":
[{"id":"x","first":"x","last":"x"}],"field_60414":["11"],"field_60375":
["22"],"field_60395":["A"],"record_id":"1920704","form_id":"10898"},
{"dt_created":"08/23/2013 04:47:45 PM","created_by":"x,
x","dt_updated":"08/23/2013 04:47:45 PM","updated_by":"x,
x","field_60374":["Project 2"],"field_60331":["Very cool project to allow
more than a single invoice to be in an ERP at any one time. Quite a big
leap forward."],"field_60333":
[{"id":"x","first":"x","last":"x"}],"field_60394":
[{"id":"x","first":"x","last":"x"}],"field_60414":["x,
x"],"field_60375":["60"],"field_60395":
["A"],"record_id":"1920703","form_id":"10898"}],
"meta":{"total":2,"count":2}
}
The format that I can't iterate through, since it is missing a top-level
name, is: [{"id":"x","user":
{"id":"x","first_name":"x","last_name":"x"},"title":"Test Post
3","text":"This is an equally cool and enthralling
post.","created_at":"2013-08-29T17:46:04.801Z","updated_at":"2013-08-29T17:46:04.804Z","num_comments":0,"num_likes":0},
{"id":"y","user":
{"id":"212342277","first_name":"x","last_name":"x"},"title":"Test Post
1","text":"Super cool content you want to read!","created_at":"2013-08-
29T17:44:58.188Z","updated_at":"2013-08-29T17:44:58.190Z","num_comments":0,"num_likes":0}]
I tried the following to massage the second JSON example into the format
of the first:
$.ajax({
[…….],
success: function(json_data){
alert("success");
var template=$('#listPosts').html();
var stuff = {"records":json_data}; //here
var stuff = JSON.stringify(stuff); //here
alert(stuff);
//var html = Mustache.to_html(template,
json_data);
var html = Mustache.to_html(template, stuff);
//$('.content-pane').empty();
$('#listOfPostsContainer').html(html);
}
}
I have an alert that pops up with what is in var 'stuff', and it seems to
be formatted like the first JSON example, but mustache.js doesn't parse it
(I have verified mustache.js executes). Before trying to address any other
issues, I wanted to understand if how I am adding a top-level name to the
JSON array in the above AJAX call success attribute is correct, or if I
have to do it differently.
If I am missing something to help explain this, let me know, so I can add it.

No comments:

Post a Comment