Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex JSON nesting of objects and arrays

I am having difficultly with syntax and structure of JSON objects/arrays.

{    "accounting" : [                         { "firstName" : "John",                          "lastName"  : "Doe",                        "age"       : 23 },                       { "firstName" : "Mary",                          "lastName"  : "Smith",                         "age"      : 32 }                  ],                               "sales"      : [                       { "firstName" : "Sally",                         "lastName"  : "Green",                         "age"      : 27 },                       { "firstName" : "Jim",                           "lastName"  : "Galley",                        "age"       : 41 }                  ]  }  

I want to make a nested structure of objects and arrays that would house the following info:

{ "problems": [{     "Diabetes":[{         "medications":[{             "medicationsClasses":[{                 "className":[{                     "associatedDrug":[{                         "name":"asprin",                         "dose":"",                         "strength":"500 mg"                     }],                     "associatedDrug#2":[{                         "name":"somethingElse",                         "dose":"",                         "strength":"500 mg"                     }]                 }],                 "className2":[{                     "associatedDrug":[{                         "name":"asprin",                         "dose":"",                         "strength":"500 mg"                     }],                     "associatedDrug#2":[{                         "name":"somethingElse",                         "dose":"",                         "strength":"500 mg"                     }]                 }]             }]         }],         "labs":[{             "missing_field": "missing_value"         }]     }],     "Asthma":[{}] }]} 

But I have no idea what the right way to do this should be. Should I just be making JavaScript objects? Does JSON make sense for this project?

What is the correct syntax to set something like this up?

Here is my code so far:

$(document).ready(function() {     $.getJSON('js/orders.json', function(json) {       $.each(json.problems, function(index, order) {         $('.loadMeds').append('<p>' + order.name + '</p>')       });     }); }); 
like image 278
Alex Avatar asked May 10 '12 18:05

Alex


People also ask

Can you nest objects in JSON?

Objects can be nested inside other objects. Each nested object must have a unique access path. The same field name can occur in nested objects in the same document.

Can you have nested arrays in JSON?

Attributes and event data can contain nested (JSON) values—arrays, objects, and arrays of objects.

What is complex JSON?

Complex JSON objects are those objects that contain a nested object inside the other. Example of Complex JSON Object.

What is JSON nested array?

Nested JSON is simply a JSON file with a fairly big portion of its values being other JSON objects. Compared with Simple JSON, Nested JSON provides higher clarity in that it decouples objects into different layers, making it easier to maintain.


1 Answers

The first code is an example of Javascript code, which is similar, however not JSON. JSON would not have 1) comments and 2) the var keyword

You don't have any comments in your JSON, but you should remove the var and start like this:

orders: { 

The [{}] notation means "object in an array" and is not what you need everywhere. It is not an error, but it's too complicated for some purposes. AssociatedDrug should work well as an object:

"associatedDrug": {                 "name":"asprin",                 "dose":"",                 "strength":"500 mg"           } 

Also, the empty object labs should be filled with something.

Other than that, your code is okay. You can either paste it into javascript, or use the JSON.parse() method, or any other parsing method (please don't use eval)

Update 2 answered:

obj.problems[0].Diabetes[0].medications[0].medicationsClasses[0].className[0].associatedDrug[0].name 

returns 'aspirin'. It is however better suited for foreaches everywhere

like image 175
Corkscreewe Avatar answered Sep 23 '22 03:09

Corkscreewe