Groovy remove from map - java

The list below contains a collection of id's
List id_wanted = ['3894586', '2786438236', '895673985']
Given the list above, How do I remove elements from the JSON below that match with the id above List?
JSON:
{
"animals": [
{
"name": "lion",
"countries": [
{
"name": "kenya",
"facts": [
{
"features": [
"young male"
],
"age": "2y",
"id": "2837492"
}
]
},
{
"name": "tanzania",
"facts": [
{
"features": [
"cub"
],
"age": "0y",
"id": "3894586"
}
]
},
{
"name": "south africa",
"facts": [
{
"features": [
"adult lioness"
],
"age": "10y",
"id": "495684576"
},
{
"features": [
"young female"
],
"age": "4y",
"id": "2786438236"
}
]
}
]
},
{
"name": "giraffe",
"countries": [
{
"name": "zambia",
"facts": [
{
"features": [
"ex captivity"
],
"age": "20y",
"id": "343453509"
}
]
},
{
"name": "kenya",
"facts": [
{
"features": [
"male"
],
"age": "17y",
"id": "85604586"
}
]
},
{
"name": "uganda",
"facts": [
{
"features": [
"young female"
],
"age": "4y",
"id": "895673985"
},
{
"features": [
"none"
],
"age": "11y",
"id": "39860394758936764"
}
]
}
]
}
]
}
For example, the following block would be removed from the JSON above because id matches with the List id_wanted
{
"features": [
"young female"
],
"age": "4y",
"id": "2786438236"
}

Assuming your json is a String in a variable inputJson, it's probably easier to build a new Json document from the original with those values filtered out:
import groovy.json.*
def json = new JsonSlurper().parseText(inputJson)
List id_wanted = ['3894586', '2786438236', '895673985']
def result = new JsonBuilder([
animals: json.animals.collect {[
name: "$it.name",
countries: it.countries.collect { [
name: "$it.name",
facts: it.facts.findAll { !(it.id in id_wanted) }
]}
]}
]).toString()

You can parse your original json and modify the resulted data structure in-place using handy *. spread operator:
def json = slurper.parseText(original)
json.animals*.countries*.facts*.each { facts ->
facts.removeAll { fact -> fact.id in id_wanted }
}
def filtered = new JsonBuilder(json).toPrettyString()
println(filtered)
Output (with facts from id_wanted removed):
{
"animals": [
{
"name": "lion",
"countries": [
{
"name": "kenya",
"facts": [
{
"features": [
"young male"
],
"age": "2y",
"id": "2837492"
}
]
},
{
"name": "tanzania",
"facts": [
]
},
{
"name": "south africa",
"facts": [
{
"features": [
"adult lioness"
],
"age": "10y",
"id": "495684576"
}
]
}
]
},
{
"name": "giraffe",
"countries": [
{
"name": "zambia",
"facts": [
{
"features": [
"ex captivity"
],
"age": "20y",
"id": "343453509"
}
]
},
{
"name": "kenya",
"facts": [
{
"features": [
"male"
],
"age": "17y",
"id": "85604586"
}
]
},
{
"name": "uganda",
"facts": [
{
"features": [
"none"
],
"age": "11y",
"id": "39860394758936764"
}
]
}
]
}
]
}

Related

select a specifique field that match a condition in Java/Spring mongo

I've a document of type:
{...
"array1": [
{
"id": "id1",
"value": "v1"
},
{
"id": "id2",
"value": "v2"
}
],
"array2": [
{
"id": "id-1",
"value": "value1"
},
{
"id": "id-2",
"value": "value2"
}
]
}
to get only the value that matches my conditions from the array1 and array2 in js we can do this,
db.mydb.find({...},{ _id: 0, "array1" : {$elemMatch: {"id" :"id1"}},"array2" : {$elemMatch: {"id" :"id-2"}}})
but I wonder how to do the same thing in Spring boot
my expected answer :
{
"array1": [
{
"id": "id1",
"value": "v1"
}
],
"array2": [
{
"id": "id-2",
"value": "value2"
}
]
}

MongoDB : fields are excluded when invoke multiple lookup stages

db={
"dashboard": [
{
"_id": "dashboard1",
"name": "test",
"user": 1
}
],
"templatefolders": [
{
"dashboardId": "dashboard1",
"folderId": "folder123",
"name": "folder",
"region": "XXX"
}
],
"folders": [
{
"_id": "folder123"
}
],
"user": [
{
"_id": 1,
"name": "alaa"
}
],
}
this is my function:
db.dashboard.aggregate([
{
"$lookup": {
"from": "templatefolders",
"localField": "_id",
"foreignField": "dashboardId",
"as": "joinDashboard"
}
},
{
"$lookup": {
"from": "folders",
"localField": "joinDashboard.folderId",
"foreignField": "_id",
"as": "joinDashboard.joinFolder"
}
},
])
Result :
[
{
"_id": "dashboard1",
"joinDashboard": {
"joinFolder": [
{
"_id": "folder123"
}
]
},
"name": "test",
"user": 1
}
]
[![enter image description here][1]][1]
Why the fields name and region in collection templatefolders are excluded ?
I want to know why this behavior ? I don't like to use unwind because i have multiple collections with multiple refrence relation.
Your second $lookup, is overriding the joinDashboard key completely. Since you want joinFolder to be within joinDashboard, you can try nested lookups like this:
db.dashboard.aggregate([
{
$lookup: {
from: "templatefolders",
let: {
"boardId": "$_id"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$dashboardId",
"$$boardId"
]
}
}
},
{
$lookup: {
from: "folders",
let: {
"folderId": "$folderId"
},
pipeline: [
{
$match: {
$expr: {
$eq: [
"$_id",
"$$folderId"
]
}
}
},
],
as: "joinFolder"
},
},
],
as: "joinDashboard"
}
}
])
MongoPlayground link.

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

How to transform row data in cassandra to nested json with respect to the columns

I have the following table in cassandra. (row level data)
The last column(ENGINE_CC) is unique. I want to categories the available data with respect to that column.
Required output in form of nested json:
{
"VECHILE_TYPE": [
{
"name": "Two Wheeler",
"MODEL": [
{
"name": "Bike",
"SUBMODEL": [
{
"name": "Super Bike",
"VECHILE_NAME": [
{
"name": "Suzuki_Hayabusa",
"Engine_CC": [
"250",
"300",
"450"
]
},
{
"name": "GSX",
"Engine_CC": [
"225",
"200"
]
}
]
},
{
"name": "Street Bike",
"VECHILE_NAME": [
{
"name": "Gixxer",
"Engine_CC": [
"175",
"150"
]
}
]
}
]
}
]
},
{
"name": "Four Wheeler",
"MODEL": [
{
"name": "Economic",
"SUBMODEL": [
{
"name": "Hatchback",
"VECHILE_NAME": [
{
"name": "I10",
"Engine_CC": [
"1000",
"1200"
]
},
{
"name": "Santro",
"Engine_CC": [
"800",
"900"
]
}
]
}
]
}
]
}
]
}
Could someone suggest
1)JAVA model for the nested json
2)How to add the row level data to the model
Is any library available to achieve this. This is basically a design question. Any suggestions ? Thanks in advance.

Categories