Accessing data in nested json
Complex JSON nesting of objects and arrays
Get nested JSON - javascript
Is there a neat way to work with deeply-nested JSON?
Videos
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
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");
});
});
Your input string is wrong
<p id="demo"></p>
<script>
var text='{ "rajaongkir":{ '
+ ' "query":{'
+' "origin":"501",'
+' "destination":"114",'
+' "weight":1700,'
+' "courier":"jne"'
+' },'
+' "status":{'
+' "code":200,'
+' "description":"OK"'
+' },'
+' "origin_details":{'
+' "city_id":"501",'
+' "province_id":"5",'
+' "province":"DI Yogyakarta",'
+' "type":"Kota",'
+' "city_name":"Yogyakarta",'
+' "postal_code":"55000"'
+' },'
+' "destination_details":{'
+' "city_id":"114",'
+' "province_id":"1",'
+' "province":"Bali",'
+' "type":"Kota",'
+' "city_name":"Denpasar",'
+' "postal_code":"80000"'
+' },'
+' "results":['
+' {'
+' "code":"jne",'
+' "name":"Jalur Nugraha Ekakurir (JNE)",'
+' "costs":['
+' {'
+' "service":"OKE",'
+' "description":"Ongkos Kirim Ekonomis",'
+' "cost":['
+' {'
+' "value":38000,' //<<<<<< GET THIS VALUE
+' "etd":"4-5",'
+' "note":""'
+' }'
+' ]'
+' },'
+' {'
+' "service":"REG",'
+' "description":"Layanan Reguler",'
+' "cost":['
+' {'
+' "value":44000,'
+' "etd":"2-3",'
+' "note":""'
+' }'
+' ]'
+' },'
+' {'
+' "service":"SPS",'
+' "description":"Super Speed",'
+' "cost":['
+' {'
+' "value":349000,'
+' "etd":"",'
+' "note":""'
+' }'
+' ]'
+' },'
+' {'
+' "service":"YES",'
+' "description":"Yakin Esok Sampai",'
+' "cost":['
+' {'
+' "value":98000,'
+' "etd":"1-1",'
+' "note":""'
+' }'
+' ]'
+' }'
+' ]'
+' }'
+' ]}}';
obj=JSON.parse(text);
document.getElementById("demo").innerHTML=
obj.rajaongkir.results[0].costs[0].cost[0].value;
</script>
Your JSON is not a valid string literal. There are no line breaks allowed, unless you end every line with a \.
If you add these \, everything is fine:
https://jsfiddle.net/ec3n6f20/