Updating elements in a Map which has immutable objects - java

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"
}
]
}
}

Related

Parse JSON document based on predefined JSON mapping document in 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.

Is there a way to use aggregation function to search for two fields for MongoDB/Java

What i would like to do is get all countries that have a region or subregion equal to "Southern Asia". Is there a function to perform compare both region and subregion to the string "Southern Asia"? I tried using filter but that didnt work out for me. The code below works fine to find regions that are equal to "Souther Asia"
Bson match = match(eq("region", "Southern Asia"));
This is an example of the json Data:
{
"_id": {
"$oid": "60c25ff18d787ab8bb976a6b"
},
"name": "Afghanistan",
"topLevelDomain": [
".af"
],
"alpha2Code": "AF",
"alpha3Code": "AFG",
"callingCodes": [
"93"
],
"capital": "Kabul",
"altSpellings": [
"AF",
"Afġānistān"
],
"region": "Asia",
"subregion": "Southern Asia",
"population": {
"$numberDouble": "27657145.0"
},
"latlng": [
{
"$numberDouble": "33.0"
},
{
"$numberDouble": "65.0"
}
],
"demonym": "Afghan",
"area": {
"$numberDouble": "652230.0"
},
"gini": {
"$numberDouble": "27.8"
},
"timezones": [
"UTC+04:30"
],
"borders": [
"IRN",
"PAK",
"TKM",
"UZB",
"TJK",
"CHN"
],
"nativeName": "افغانستان",
"numericCode": "004",
"currencies": [
{
"code": "AFN",
"name": "Afghan afghani",
"symbol": "؋"
}
],
"languages": [
{
"iso639_1": "ps",
"iso639_2": "pus",
"name": "Pashto",
"nativeName": "پښتو"
},
{
"iso639_1": "uz",
"iso639_2": "uzb",
"name": "Uzbek",
"nativeName": "Oʻzbek"
},
{
"iso639_1": "tk",
"iso639_2": "tuk",
"name": "Turkmen",
"nativeName": "Türkmen"
}
],
"translations": {
"de": "Afghanistan",
"es": "Afganistán",
"fr": "Afghanistan",
"ja": "アフガニスタン",
"it": "Afghanistan",
"br": "Afeganistão",
"pt": "Afeganistão",
"nl": "Afghanistan",
"hr": "Afganistan",
"fa": "افغانستان"
},
"flag": "https://restcountries.eu/data/afg.svg",
"regionalBlocs": [
{
"acronym": "SAARC",
"name": "South Asian Association for Regional Cooperation",
"otherAcronyms": [],
"otherNames": []
}
],
"cioc": "AFG"
}
Use the or filter like this:
Bson match = match(or(eq("region", "Southern Asia"), eq("subregion", "Southern Asia")));

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"
}
}

Jolt transform unnamed array

Using the sample json response from jsonplaceholder.com, I would like to perform a Jolt transformation on the unnamed array that is returned.
However, using the jolt demo I have only been able to transform the array after naming the array ("records" in this example), and wrapping with curly braces. Like this:
Json Input:
{
"records": [
{
"id": 1,
"name": "Leanne Graham",
"username": "Bret",
"email": "Sincere#april.biz",
"address": {
"street": "Kulas Light",
"suite": "Apt. 556",
"city": "Gwenborough",
"zipcode": "92998-3874",
"geo": {
"lat": "-37.3159",
"lng": "81.1496"
}
},
"phone": "1-770-736-8031 x56442",
"website": "hildegard.org",
"company": {
"name": "Romaguera-Crona",
"catchPhrase": "Multi-layered client-server neural-net",
"bs": "harness real-time e-markets"
}
},
{
"id": 2,
"name": "Ervin Howell",
"username": "Antonette",
"email": "Shanna#melissa.tv",
"address": {
"street": "Victor Plains",
"suite": "Suite 879",
"city": "Wisokyburgh",
"zipcode": "90566-7771",
"geo": {
"lat": "-43.9509",
"lng": "-34.4618"
}
},
"phone": "010-692-6593 x09125",
"website": "anastasia.net",
"company": {
"name": "Deckow-Crist",
"catchPhrase": "Proactive didactic contingency",
"bs": "synergize scalable supply-chains"
}
}
]
}
Jolt Spec:
[
{
"operation": "shift",
"spec": {
"records": {
"*": {
"id": "records[&1].user-id",
"username": "records[&1].user-username",
"email": "records[&1].user-email",
"address": {
"street": "records[&2].user-street",
"suite": "records[&2].user-suite",
"city": "records[&2].user-city",
"zipcode": "records[&2].user-zipcode"
}
}
}
}
}
]
My example's goal is to flatten the object hierarchy returned in the response, while maintaining the basic [{}, {}, ...] structure.
How can I achieve this when the input is an unnamed json array?
I just realized the answer after posting...
Here is my revised spec for anyone that is interested:
[
{
"operation": "shift",
"spec": {
"*": {
"id": "[&1].user-id",
"username": "[&1].user-username",
"email": "[&1].user-email",
"address": {
"street": "[&2].user-street",
"suite": "[&2].user-suite",
"city": "[&2].user-city",
"zipcode": "[&2].user-zipcode"
}
}
}
}
]
Simply match after declaring spec ("spec": { "*": { "), then access the unnamed array with [&1], [&2], etc
below one works, but not flattens
[
{
"operation": "shift",
"spec": {
"*": {
"*": {
"id": "[&1].user-id",
"username": "[&1].user-username",
"email": "[&1].user-email",
"address": {
"street": "[&2].user-street",
"suite": "[&2].user-suite",
"city": "[&2].user-city",
"zipcode": "[&2].user-zipcode"
}
}
}
}
}
]

Precondition on data pipeline

I did some set of activities on json script. Need to add precondition on it.
how to use precondition on json script.
In precondition, i need to check the mysql db field. once flag as 'Y',
start execution else should be stop the execution.
You can use a ShellCommandPrecondition for this which will let you write a custom precondition.
{
"objects": [
{
"schedule": {
"ref": "DefaultSchedule"
},
"resourceRole": "DataPipelineDefaultResourceRole",
"role": "DataPipelineDefaultRole",
"name": "DefaultResource1",
"id": "ResourceId_dWoZ0",
"type": "Ec2Resource",
"terminateAfter": "1 Hour"
},
{
"name": "DefaultPrecondition1",
"id": "PreconditionId_yA2rV",
"type": "ShellCommandPrecondition",
"command": "<Script to check mysql field>"
},
{
"occurrences": "1",
"period": "1 Day",
"name": "RunOnce",
"id": "DefaultSchedule",
"type": "Schedule",
"startAt": "FIRST_ACTIVATION_DATE_TIME"
},
{
"failureAndRerunMode": "CASCADE",
"schedule": {
"ref": "DefaultSchedule"
},
"resourceRole": "DataPipelineDefaultResourceRole",
"role": "DataPipelineDefaultRole",
"pipelineLogUri": "s3://<mybucket>",
"scheduleType": "cron",
"name": "Default",
"id": "Default"
},
{
"schedule": {
"ref": "DefaultSchedule"
},
"name": "DefaultActivity1",
"runsOn": {
"ref": "ResourceId_dWoZ0"
},
"precondition": {
"ref": "PreconditionId_yA2rV"
},
"id": "ActivityId_gmQ0W",
"type": "ShellCommandActivity",
"command": "echo 'Hello world'"
}
],
"parameters": []
}

Categories