Parse JSON document based on predefined JSON mapping document in Java - java

I want to get some idea if there is any library in Java that I can use to filter a JSON document based on a predefined JSON mapping document, or only way to achieve this is to write custom java code.
If it's required to write Java custom code what type of design pattern or data structure I should follow. Any advice will be very much appreciated.
Example -
Input JSON document -
{
"basicInfo": {
"name": "name",
"age": "25",
"address": "address"
},
"education": [
{
"ug": {
"unversity": "university",
"major": [
"cs",
"ds"
],
"year": "2012"
},
"pg": {
"unversity": "university",
"major": [
"cs",
"ds"
],
"year": "2015"
}
}
]
}
Predefined JSON mapping document (Can be defined in any JSON format, below is one such example that I have created) -
{
"definitions": {
"basicInfo": {
"maxOccurance": "1",
"fields": [
{
"key": "name",
"type": "S",
"lenght": "50",
"usage": "M",
"maxOccurrance": "1"
},
{
"key": "age",
"type": "S",
"lenght": "3",
"usage": "O",
"maxOccurrance": "1"
}
]
},
"education": {
"maxOccurance": "10",
"fields": [
{
"pg": {
"maxOccurance": "1",
"fields": [
{
"key": "university",
"type": "S",
"lenght": "50",
"usage": "M",
"maxOccurrance": "1"
},
{
"key": "major",
"type": "S",
"lenght": "3",
"usage": "O",
"maxOccurrance": "1"
}
]
}
}
]
}
}
}
Expected output JSON document
{
"basicInfo": {
"name": "name",
"age": "25"
},
"education": [
{
"pg": {
"unversity": "university",
"major": "cs"
}
}
]
}

The request is for a way to filter a JSON document based on a predefined JSON spec sheet / JSON mapping document. The jolt library would likely be able to meet this requirement.

Related

Updating elements in a Map which has immutable objects

I have a Java Map which is deserialized into immutable objects from a json similar to below and has multiple levels of nesting. I have a second Map containing alternate values for some of the strings. The second map has the value appended to the key for the nested levels to differentiate between different elements in an array. I would like to replace the values in the first map using the values found in the second map. Are there any suggestions for the approach that could used to solve the below?
{
"product": "Internet",
"version": 3.1,
"releaseDate": "2014-06-25T00:00:00.000Z",
"person": {
"id": 12345,
"name": "John Doe",
"phones": {
"home": "800-123-4567",
"mobile": "877-123-1234"
},
"email": [
"jd#example.com",
"jd#example.org"
],
"dateOfBirth": "1980-01-02T00:00:00.000Z",
"registered": true,
"emergencyContacts": [
{
"name": "Jane Doe",
"phone": "888-555-1212",
"relationship": "spouse"
},
{
"name": "Justin Doe",
"phone": "877-123-1212",
"relationship": "parent"
}
]
}
}
Map containing alternate values
{
"product": "Strong Internet",
"person.name=John Doe" : "Barry Richards",
"person.emergencyContacts.name=Jane Doe" : "Jane Richards"
}
Resulting Map
{
"product": "Strong Internet",
"version": 3.1,
"releaseDate": "2014-06-25T00:00:00.000Z",
"person": {
"id": 12345,
"name": "Barry Richards",
"phones": {
"home": "800-123-4567",
"mobile": "877-123-1234"
},
"email": [
"jd#example.com",
"jd#example.org"
],
"dateOfBirth": "1980-01-02T00:00:00.000Z",
"registered": true,
"emergencyContacts": [
{
"name": "Jane Richards",
"phone": "888-555-1212",
"relationship": "spouse"
},
{
"name": "Justin Doe",
"phone": "877-123-1212",
"relationship": "parent"
}
]
}
}

How to form post request body where resource contained multi reference resources from Java to FHIR api

I have to send request body as org.hl7.fhir.r4.model.CoverageEligibilityRequest which is contained Patient, Practitioner, Organization as below. This API will return Bundle response. I am using Java and Generic client from Hapi Fire library. There is provision to pass search parameter but the here I am having multilevel hierarchy of resource and input is big than usual. Can any one help me to handle this request in FHIR API.
Request Body as below,
{
"resourceType": "CoverageEligibilityRequest",
"contained": [
{
"resourceType": "Patient",
"id": "1",
"name": [
{
"family": "abcFamily",
"given": [
"abcGiven"
]
}
],
"birthDate": "1962-08-06"
},
{
"resourceType": "Practitioner",
"id": "2",
"identifier": [
{
"type": {
"coding": [
{
"code": "NPI"
}
]
},
"value": "123456789"
}
],
"name": [
{
"family": "pqrFamily",
"given": [
"pqrGiven"
]
}
]
},
{
"resourceType": "Organization",
"id": "3",
"identifier": [
{
"value": "12345"
}
],
"name": ""
},
{
"resourceType": "Coverage",
"id": "3",
"status": "active",
"subscriberId": "",
"class": [
{
"type": {
"coding": [
{
"code": "group"
}
]
},
"value": ""
}
]
}
],
"extension": [
{
"url": "searchOption",
"valueString": "NameDateOfBirth"
}
],
"status": "active",
"purpose": [
"benefits"
],
"patient": {
"reference": "#1"
},
"provider": {
"reference": "#2"
},
"insurer": {
"reference": "#3"
}
}

How to parse a JSON with SCIM Schemas in java

I have the following json which get from endpoint,
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:response",
"urn:scim:schemas:extension:customattrs:2.0:User"
],
"meta": {
"resourceType": "User",
"created": "2011-08-01T18:29:49.793Z",
"lastModified": "2011-08-01T18:29:49.793Z",
"version": "W/\"f250dd84f0671c3\""
},
"userName": "bjensen",
"phoneNumbers": [
{
"value": "555-555-8377",
"type": "work"
}
],
"emails": [
{
"value": "bjensen#example.com",
"type": "work",
"primary": true
}
],
"urn:scim:schemas:extension:customattrs:2.0:User": {
"userName": "bjensen",
"address": ""
}
}
please suggest me to parse the json along with schemas "urn:scim:schemas:extension:customattrs:2.0:User" into java object

How to parse a DiagnosticReport from a JSON and print the same JSON again?

I'm parsing a DiagnosticReport from a JSON file and It works fine, but when I try to print the same JSON file throught IParser encode function, the JSON is different to the original. I need to print the same JSON.
Original JSON (String json)
{
"resourceType": "DiagnosticReport",
"text": {
"status": "generated",
"div": "<div><p><b>Narrative A</b></p></div>"
},
"contained": [
{
"resourceType": "Patient",
"id": "1"
},
{
"resourceType": "Observation",
"id": "2",
"meta": {
"lastUpdated": "2017-03-22T22:00:28.089-05:00"
},
"text": {
"div": "<div><p><b>Narrative B</b></p></div>"
},
"comment": "a comment"
}
],
"status": "appended",
"code": {
"coding": [
{
"code": "Report01"
}
]
},
"subject": {
"reference": "#1"
},
"effectiveDateTime": "2017-03-22T22:00:28-05:00",
"issued": "2017-03-22T22:00:28.070-05:00",
"result": [
{
"reference": "#2"
}
]
}
First step is parse and the second step is encode and print
DiagnosticReport report = parser.parseResource(DiagnosticReport.class, json);
String encodeJSON = parser.encodeResourceToString(report);
System.out.println(encodeJSON);
And the result is different because the text tag in the Observation is not showed
{
"resourceType": "DiagnosticReport",
"text": {
"status": "generated",
"div": "<div xmlns=\"http://www.w3.org/1999/xhtml\"><p><b>Narrative A</b></p></div>"
},
"contained": [
{
"resourceType": "Patient",
"id": "1"
},
{
"resourceType": "Observation",
"id": "2",
"meta": {
"lastUpdated": "2017-03-22T22:00:28.089-05:00"
},
"comment": "a comment"
}
],
"status": "appended",
"code": {
"coding": [
{
"code": "Report01"
}
]
},
"subject": {
"reference": "#1"
},
"effectiveDateTime": "2017-03-22T22:00:28-05:00",
"issued": "2017-03-22T22:00:28.070-05:00",
"result": [
{
"reference": "#2"
}
]
}
I'm trying this because I have a DiagnosticReport generated by my software and I need print it completely in a JSON file.
Thanks for your help!!
It's not legal to have narrative in a contained resource, nor is it legal to have meta/lastUpdated. There are invariants that prohibit both. Ideally, the parsing software should have thrown an exception, but it's not overly surprising that the serializer has trouble serializing content that's not supposed to be there.
Look at dom-1 and dom-4 in dstu3 or dstu2

How to retrieve an element value from json using java?

Here is the json string from which i have to retrieve the element value
{ "search": { "entry": [
{ "d": "op=example.com",
"at": [
{ "name": "id", "value": [ "786786876" ] },
{ "name": "name", "value": [ "usaduas" ] }]
},
{ "d": "op=example.com",
"at": [
{ "name": "id", "value": [ "786876876" ] },
{ "name": "call", "value": [ "gyudyusg" ] }]
},
{ "d": "op=example.com",
"at": [
{ "name": "call", "value": [ "hsadkjhsakjdh" ] },
{ "name": "id", "value": [ "768768768" ] }]
}
I want to retrieve the text from attribute or node "name" : "call". I want to retrieve this value using jsonPath.
Thanks in advance.
please try one of these as per your understanding. These two are most convenient and highly used API to Serialize or Deserialize the JSONObject google gson API tutorial-convert-java-object-to-from-json
OR
Google Json simple API example-read-and-write-json

Categories