This may be a noob question, but if I wanted to make a JSON list of items (in a nodejs app) I can do the following:
var myVar = {
'title' : 'My Title',
'author' : 'A Great Author'
};
console.log(JSON.stringify(myVar));
OUTPUT: { 'title' : 'My Title', 'author' : 'A Great Author' }
and everything works great, but how do I make a sublist like the following?
OUTPUT: { book {'title' : 'My Title', 'author' : 'A Great Author'} }
{}
is the object literal syntax, propertyName: propertyValue
defines a property. Go ahead and nest them.
var myVar = {
book: {
'title' : 'My Title',
'author' : 'A Great Author'
}
};
To do it with JavaScript:
var mVar = {
'title' : 'My Title',
'author' : 'A Great Author'
};
var myVar = {};
myVar.book = mVar;
console.log(JSON.stringify(myVar));
See: http://jsfiddle.net/JvFQJ/
To do it using the object literal notation:
var myVar = {
'book': {
'title' : 'My Title',
'author' : 'A Great Author'
}
};
console.log(JSON.stringify(myVar));
As so:
var myVar = {
'book': {
'title' : 'My Title',
'author' : 'A Great Author'
}
};
You'd do something like this:
myVar = {
book: {
'title' : 'My Title',
'author' : 'A Great Author'
}
}
console.log(JSON.stringify(myVar)); // OUTPUT: { book {'title' : 'My Title', 'author' : 'A Great Author'} }
If you wanted multiple items in the sublist, you'd change it to this:
myVar = {
book1: {
'title' : 'My Title',
'author' : 'A Great Author'
},
book2: {
'title' : 'My Title',
'author' : 'A Great Author'
}
}