FHIR printing json issue


#1

Hi team,
I am creating an FHIR Appointment resource. I added the attributes into it. When I use pretty print json, it is printing the contents in JSON. But the moment I added contained resources within appointment, it is printing without the contained resource. Any Help would be greatly appreciated.


#2

What software are you using? What version?


#3

Hi llyod, Sorry for the late response. I am using FHIR Dtsu3 . When am doing the print, It is neither printing participant component or contained resource. But it’s printing other components within the resource. Please guide me . What am I missing.


#4

That’s what version of the standard you’re using. My question is what software are you using? Are you using the Java reference implementation? The C# reference implementation? Some other library? What does the relevant code say?


#5

Hi Lloyd,
Sorry for getting back late. I am using hapi-fhir-structures-dstu3 jar in Java. I am able to parse and get all elements in the Appointment Resource except the contained segment. Please help to correct me.

{
“resourceType”: “Appointment”,
“identifier”: [
{
“id”: “2222”,
“type”: {
“text”: “AppointmentId”
}
},
{
“id”: “222222222”,
“type”: {
“text”: “CaseNumber”
}
}

],
“status”: “booked”,
“serviceCategory”: {
“code”: “27”,
“text”: “specialist medical”
},
“serviceType”: [
{
“text”: “sub specialty”
}
],
“specialty”: [
{
“coding”: [
{
“display”: “speciality”
}
]
}
],
“description”:“SMS:1|EMAIL:1|PN:1|SMSSENDER:XX|SMSRECEIVER:91234567|SMSTEXT:This is a XX alert|EMAILSENDER:XXX|EMAILRECEIVER:wwtest@abc.com|EMAILSUBJECT:Welcome|EMAILTEXT:Welcome
PNSENDER:XXX|PNSUBJECT:EMAILSUBJECT:Welcome|PNTEXT:Welcome”,
“reason”: [
{
“coding”: [
{
“display”: “diagnosis”
}
]
}
],
“priority”:0,
“participant”: [
{
“actor”: {
“reference”: “#Patient-1
},
“status”: “accepted”
},
{
“actor”: {
“reference”: “#Practitioner-1
},
“status”: “accepted”
},
{
“actor”: {
“reference”: “#Location-1
},
“status”: “accepted”
}
],
“start”: “2018-02-21T07:10:00.000+08:00”,
“created”: “2018-02-21T07:00:14.388+08:00”,
“comment”: “disposition code & action description”,
“incomingReferral”: [
{
“reference”: “Referral Information”,
“display”:“Doctor Name|Hospital|Description”
},
{
“reference”: “Communication Preference”,
“display”:“Phone”
},
{
“reference”: “Notification”,
“display”:“SMS,Confirmation Only|Email,Reminder Only|PN,Confirmation and Reminder”
}

],
“contained”:
[
{
“resourceType”: “Patient”,
“id”: “#Patient-1”,
“identifier”: [
{
“id”: “2222”,
“type”: {
“text”: “NRIC”
}
},
{
“id”: “2222”,
“type”: {
“text”: “PatientId”
}
}

],
“name”: [
{
“family”: “Vijay”,
“given”: [
“Peter”,
“James”
]
}
],
“prefix”:“Mr”,
“gender” : “male”,
“telecom” : [
{
“system”: “phone”,
“value”: “(03) 5555 6473”
},
{
“system”: “email”,
“value”: "xxx@gmail.com"
}
],
“birthDate”: “2018-02-21”
},
{
“resourceType” : “Practitioner”,
“id”: “#Practitioner-1”,
“identifier”: [
{
“id”: “2222”,
“type”: {
“text”: “MCR”
}
}
],
“name”: [
{
“family”: " Dr Vijay",
“given”: [
“Peter”,
“James”
]
}
]

},
{
“resourceType” : “Location”,
“id”: “#Location-1”,
“name” : “MEHospital”,
“identifier”: [
{
“id”: “2222”,
“type”: {
“text”: “Hospital/Clinic code”
}
}
],
“address”: [
{
“use”: “home”,
“type”: “both”,
“text”: “534 Erewhon St PeasantVille, Rainbow, Vic 3999”,
“line”: [
“534 Erewhon St”
],
“city”: “PleasantVille”,
“district”: “Rainbow”,
“state”: “Vic”,
“postalCode”: “3999”
}
],
“telecom” : [
{
“system”: “phone”,
“value”: “(03) 5555 6473”
},
{
“system”: “email”,
“value”: "xxx@gmail.com"
}
]

}

]
}


#6

Ok, that’s the instance. Can you share the code that’s not working?


#7

Hi Lloyd,
Here is the code. I am adding a contained resource .But couldn’t see my json having the contained resource. See the last line printing the entire appointment resource . But it doesn’t print contained resource. But I can get the reference to the contained resource. Please help me print the json with the contained resource.

import java.io.ByteArrayOutputStream;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hl7.fhir.dstu3.model.Appointment;
import org.hl7.fhir.dstu3.model.Appointment.AppointmentStatus;
import org.hl7.fhir.dstu3.model.AppointmentResponse;
import org.hl7.fhir.dstu3.model.Bundle;
import org.hl7.fhir.dstu3.model.CodeableConcept;
import org.hl7.fhir.dstu3.model.ContactPoint;
import org.hl7.fhir.dstu3.model.HumanName;
import org.hl7.fhir.dstu3.model.Identifier;
import org.hl7.fhir.dstu3.model.Patient;
import org.hl7.fhir.dstu3.model.Patient.ContactComponent;
import org.hl7.fhir.dstu3.model.Period;
import org.hl7.fhir.dstu3.model.Reference;
import org.hl7.fhir.dstu3.model.Resource;
import org.hl7.fhir.dstu3.model.codesystems.AdministrativeGender;
import org.hl7.fhir.dstu3.model.codesystems.ContactPointSystem;
import org.hl7.fhir.dstu3.model.codesystems.ServiceCategory;
import org.hl7.fhir.dstu3.model.codesystems.ServiceType;
import org.json.simple.parser.JSONParser;
import org.omg.CORBA.portable.ApplicationException;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.stream.JsonWriter;

import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.model.api.IResource;
import ca.uhn.fhir.parser.IParser;
import ca.uhn.fhir.parser.JsonParser;
import ca.uhn.fhir.parser.json.JsonLikeStructure;
//import ca.uhn.fhir.context.FhirContext;
import ca.uhn.fhir.rest.api.MethodOutcome;
import ca.uhn.fhir.rest.client.api.IGenericClient;

public class FHIRTest {
public static void main(String args[])
{

	FhirContext fhirContext = FhirContext.forDstu3();
	CodeableConcept idType=new CodeableConcept();
	idType.setText("AppointmentId");
	
	Identifier i=new Identifier();
	i.setId("2222");
	i.setType(idType);
	CodeableConcept serviceType=new CodeableConcept();
	serviceType.setText("Rehab Appointment");
	
	CodeableConcept serviceCategory=new CodeableConcept();
	serviceCategory.setText("Rehab Appointment");
	
	CodeableConcept appointmentType=new CodeableConcept();
	appointmentType.setText("Rehab Appointment");
	
	
	
	Appointment appointment = new Appointment();
	
	appointment.setStatus(AppointmentStatus.BOOKED);
	appointment.setComment("Start");
	appointment.addIdentifier(i);
    appointment.addServiceType(serviceType);
    appointment.setServiceCategory(serviceCategory);
    
    Date startDate=new Date();
    startDate.setMinutes(60);
    startDate.setDate(21);
    startDate.setHours(6);
    
    Date endDate=new Date();
    endDate.setMinutes(60);
    endDate.setDate(21);
    endDate.setHours(6);
    
    Period p=new Period();
    
    p.setStart(startDate);
    p.setEnd(endDate);
    
    ArrayList<Period> requestPeriods =new ArrayList<Period>();
    requestPeriods.add(p);
    
    
    appointment.setRequestedPeriod(requestPeriods);
    
    appointment.setStart(startDate);
    appointment.setEnd(endDate);
    appointment.setAppointmentType(appointmentType);
    
    
    ArrayList aps=new ArrayList();
    
    Appointment.AppointmentParticipantComponent ap=new Appointment.AppointmentParticipantComponent();
    Reference s=new Reference();
    s.setReference("2222");
    ap.setActor(s);
    aps.add(ap);
    appointment.setParticipant(aps);
    
    
    

    
    
    Patient patient=new Patient();
    patient.addIdentifier(i);
    HumanName hn=new HumanName();
    hn.setFamily("Vijay");
   
     patient.addName(hn);
    patient.setBirthDate(endDate);
    ContactPoint ct=new ContactPoint();
    ct.setSystem(ContactPoint.ContactPointSystem.PHONE);
    patient.addTelecom(ct);

    
    JSONParser parser = new JSONParser();
    

    appointment.addContained(patient);
    
   
    
    
    
    
    
    
    System.out.println("Appointment JSon::"+fhirContext.newJsonParser().setPrettyPrint(true).encodeResourceToString(appointment));

#8

Hi Vijay,

There is a tutorial here on how to create contained resources using HAPI: http://hapifhir.io/doc_resource_references.html

Cheers,
James