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
Answer from Corkscreewe on Stack OverflowThe 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
I successfully solved my problem. Here is my code:
The complex JSON object:
{
"medications":[{
"aceInhibitors":[{
"name":"lisinopril",
"strength":"10 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"antianginal":[{
"name":"nitroglycerin",
"strength":"0.4 mg Sublingual Tab",
"dose":"1 tab",
"route":"SL",
"sig":"q15min PRN",
"pillCount":"#30",
"refills":"Refill 1"
}],
"anticoagulants":[{
"name":"warfarin sodium",
"strength":"3 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"betaBlocker":[{
"name":"metoprolol tartrate",
"strength":"25 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"diuretic":[{
"name":"furosemide",
"strength":"40 mg Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}],
"mineral":[{
"name":"potassium chloride ER",
"strength":"10 mEq Tab",
"dose":"1 tab",
"route":"PO",
"sig":"daily",
"pillCount":"#90",
"refills":"Refill 3"
}]
}
],
"labs":[{
"name":"Arterial Blood Gas",
"time":"Today",
"location":"Main Hospital Lab"
},
{
"name":"BMP",
"time":"Today",
"location":"Primary Care Clinic"
},
{
"name":"BNP",
"time":"3 Weeks",
"location":"Primary Care Clinic"
},
{
"name":"BUN",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Cardiac Enzymes",
"time":"Today",
"location":"Primary Care Clinic"
},
{
"name":"CBC",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Creatinine",
"time":"1 Year",
"location":"Main Hospital Lab"
},
{
"name":"Electrolyte Panel",
"time":"1 Year",
"location":"Primary Care Clinic"
},
{
"name":"Glucose",
"time":"1 Year",
"location":"Main Hospital Lab"
},
{
"name":"PT/INR",
"time":"3 Weeks",
"location":"Primary Care Clinic"
},
{
"name":"PTT",
"time":"3 Weeks",
"location":"Coumadin Clinic"
},
{
"name":"TSH",
"time":"1 Year",
"location":"Primary Care Clinic"
}
],
"imaging":[{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
},
{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
},
{
"name":"Chest X-Ray",
"time":"Today",
"location":"Main Hospital Radiology"
}
]
}
The jQuery code to grab the data and display it on my webpage:
$(document).ready(function() {
var items = [];
$.getJSON('labOrders.json', function(json) {
$.each(json.medications, function(index, orders) {
$.each(this, function() {
$.each(this, function() {
items.push('<div class="row">'+this.name+"\t"+this.strength+"\t"+this.dose+"\t"+this.route+"\t"+this.sig+"\t"+this.pillCount+"\t"+this.refills+'</div>'+"\n");
});
});
});
$('<div>', {
"class":'loaded',
html:items.join('')
}).appendTo("body");
});
});
Creating Nested JSON Objects
Is there a neat way to work with deeply-nested JSON?
Accessing data in nested json
Build a double nested JSON object
Videos
My app uses deeply-nested JSON fetched from an API. My problem is that the structure of the JSON really does not fit what I need as I have to mix & match data from various levels of the JSON.
The approach I've taken so far is to decode the JSON into Decodable structs and then use helper functions to turn these Decodable structs into the structs that I actually need. I'm not a fan of this approach because it looks messy but I understand that for Decodable to do its job, the nesting structure of the Decodable structs has to match that of the JSON. Therefore, I can't just decode straight into the structs that I need and I have to go via these Decodable 'intermediaries'.
Am I missing something? Is there a better approach? I really hope so because this feels terribly wasteful, but my research so far hasn't yielded great results.
Edit: just to be clear - it's not the decoding itself that's the problem, I know how to use Decodable (especially with the help of quicktype!). I'm just trying to minimise the amount of Decodable structs that I need to use to decode multi-level JSON. From the comments thus far it just seems like that's not really an option.
This is what I want to generate to match a required file format (notice the extra level of brackets):
{
"content": [{
"eventType": "view",
"othervar": "new"
}]
}sample code:
import json
jsondata = {}
content={}
content['eventType'] = 'view'
content['othervar'] = "new"
jsondata['content'] = content
print(json.dumps(jsondata, indent=4))Current output:
{
"content": {
"eventType": "view",
"othervar": "new"
}
} EDIT: Thanks so much. I come from a long R background and am still learning the Python details.
There is no theoretical limit to how deep JSON objects can be nested, but there usually is a practical limit based on the decoder being used. For example, PHP's json_decode() has a default limit of 512 levels, though it can be adjusted. Read the documentation for the code using the JSON to determine the max depth.
If your JSON is actually hitting depth limits, you probably need to reconsider how your JSON is structured.
Many JSON parsers and formatters use recursion (i.e., ArduinoJSON and the IBM DastaPower Gateway).
This means that deeply nested JSON objects can be used to attack implementations that use this approach (i.e. see this older jansson issue)).
Originally, the JSON testing suite offered by json.org was testing to make sure parsers would fail at 22 levels of nesting.
This also makes sense where data accessing is concerned. Accessing nested data is less readable (and less maintainable) than multiple storage objects.
So, although there's no official limits to JSON nesting levels, in order to allow your data to be portable and safe, I would not recommend any nesting level above 16.
IMHO, the 16-31 nesting levels should be considered a hazard zone and anything above 32 nesting levels should be considered as a design error.