Stream: smart
Topic: What lib ppl using to parse and display JSON data?
Del D (Jun 02 2021 at 15:09):
I am slowly learnig how to use SMART Apps for FHIR. When you get your JSON response - what are people using to parse the JSON data and then print it out into HTML ? Below is (fake) JSON data - say I want to only echo/print a list of the patients medications taken from field "text" or field "display".. how are people doing it?
[
{
"fullUrl": "https://launch.smarthealthit.org/v/r4/fhir/MedicationRequest/83b6c511-8b78-4fe2-b484-346ddee61933",
"resource": {
"resourceType": "MedicationRequest",
"id": "83b6c511-8b78-4fe2-b484-346ddee61933",
"meta": {
"versionId": "4",
"lastUpdated": "2021-04-06T03:14:44.834-04:00",
"tag": [
{
"system": "https://smarthealthit.org/tags",
"code": "synthea-5-2019"
}
]
},
"status": "active",
"intent": "order",
"medicationCodeableConcept": {
"coding": [
{
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": "316049",
"display": "Hydrochlorothiazide 25 MG"
}
],
"text": "Hydrochlorothiazide 25 MG"
},
"subject": {
"reference": "Patient/2cda5aad-e409-4070-9a15-e1c35c46ed5a"
},
"encounter": {
"reference": "Encounter/ffbebef5-2bc6-42ed-9e45-5913752d72c4"
},
"authoredOn": "2011-05-27T09:51:38+00:00",
"requester": {
"reference": "Practitioner/8b85559d-27de-4d4b-bea7-5a715164cb60"
},
"reasonReference": [
{
"reference": "Condition/0dc3b17a-1180-4881-bbc3-6f2a399901ae"
}
]
},
"search": {
"mode": "match"
},
"response": {
"status": "200 OK",
"etag": "W/\"4\""
}
},
{
"fullUrl": "https://launch.smarthealthit.org/v/r4/fhir/MedicationRequest/f3a6c1f0-3896-4120-a186-920e593b305e",
"resource": {
"resourceType": "MedicationRequest",
"id": "f3a6c1f0-3896-4120-a186-920e593b305e",
"meta": {
"versionId": "4",
"lastUpdated": "2021-04-06T03:14:40.555-04:00",
"tag": [
{
"system": "https://smarthealthit.org/tags",
"code": "synthea-5-2019"
}
]
},
"status": "stopped",
"intent": "order",
"medicationCodeableConcept": {
"coding": [
{
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": "562251",
"display": "Amoxicillin 250 MG / Clavulanate 125 MG Oral Tablet"
}
],
"text": "Amoxicillin 250 MG / Clavulanate 125 MG Oral Tablet"
},
"subject": {
"reference": "Patient/2cda5aad-e409-4070-9a15-e1c35c46ed5a"
},
"encounter": {
"reference": "Encounter/25e95ec9-97e5-4385-8f56-b2bf6552f466"
},
"authoredOn": "2014-10-04T09:51:38+00:00",
"requester": {
"reference": "Practitioner/8b85559d-27de-4d4b-bea7-5a715164cb60"
},
"reasonReference": [
{
"reference": "Condition/d918664d-21be-4ecd-b98e-1195fd1961ac"
}
]
},
"search": {
"mode": "match"
},
"response": {
"status": "200 OK",
"etag": "W/\"4\""
}
},
{
"fullUrl": "https://launch.smarthealthit.org/v/r4/fhir/MedicationRequest/d7977dfa-dd7d-4390-b525-12e758f28c7b",
"resource": {
"resourceType": "MedicationRequest",
"id": "d7977dfa-dd7d-4390-b525-12e758f28c7b",
"meta": {
"versionId": "4",
"lastUpdated": "2021-04-06T03:05:42.212-04:00",
"tag": [
{
"system": "https://smarthealthit.org/tags",
"code": "synthea-5-2019"
}
]
},
"status": "stopped",
"intent": "order",
"medicationCodeableConcept": {
"coding": [
{
"system": "http://www.nlm.nih.gov/research/umls/rxnorm",
"code": "562251",
"display": "Amoxicillin 250 MG / Clavulanate 125 MG Oral Tablet"
}
],
"text": "Amoxicillin 250 MG / Clavulanate 125 MG Oral Tablet"
},
"subject": {
"reference": "Patient/2cda5aad-e409-4070-9a15-e1c35c46ed5a"
},
"encounter": {
"reference": "Encounter/1c93688c-eb1c-40c6-a334-cc303ad478ff"
},
"authoredOn": "2016-09-27T09:51:38+00:00",
"requester": {
"reference": "Practitioner/8b85559d-27de-4d4b-bea7-5a715164cb60"
},
"reasonReference": [
{
"reference": "Condition/d918664d-21be-4ecd-b98e-1195fd1961ac"
},
{
"reference": "Condition/9ada3825-6def-4ed3-bd27-20dd3f14472d"
},
{
"reference": "Condition/088d38d6-6d8e-415c-9073-bae8e606ac06"
},
{
Del D (Jun 02 2021 at 15:56):
If it is as simple as using JSON.parse() I have tried the following but get an error:
const obj = JSON.parse(meds);
document.getElementById("medsJSONparse").innerHTML = obj.text + ", " + obj.display;
console error:
VM15:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
with "meds" being referred to in this JS snippet:
// Render the current patient's medications (or any error)
.then(
function(meds) {
document.getElementById("meds").innerText = JSON.stringify(meds, null, 4);
},
function(error) {
document.getElementById("meds").innerText = error.stack;
}
Josh Mandel (Jun 02 2021 at 19:29):
It might help to do something like
console.log(meds)
before you try to parse it -- perhaps the value doesn't contain the string you think it does. You can also use the debugger in your browser to inspect this while your code runs, by setting a breakpoint (https://www.youtube.com/watch?v=yMEpIOyoizU has background on this).
Josh Mandel (Jun 02 2021 at 19:30):
Also when pasting large chunks of text into Zulip, take a look at https://zulip.com/help/format-your-message-using-markdown#spoilers for a handy way to collapse them :-)
Last updated: Apr 12 2022 at 19:14 UTC