I would like to know how to unset or destroy a level of JSON object.
Is this can be done using any Java Spring framework or could be define any custom functionality for this solution.
But require to retain the data inside array of inside of "txtFrameArr" as below.Example i have this result :
[
{
"txtFrameArr": {
"CEPDuplicateTextFrame1": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "999",
"name": "MARKETING_CLAIM",
"IS": "1",
"id": "CE-MARKETING_CLAIM-1_999_1_1"
}
],
"mapCordinates": [
{
"top": 468.645102009805,
"left": 1053.47265625,
"width": 30.8357511971699,
"height": 132.963461384805
},
{
"top": 513.29412308266,
"left": 1067.05751323149,
"width": 91.1260246588899,
"height": 233.24627152016
}
],
"duplicate": 1,
"content": "baby-dry pants",
"txtframe_name": "ceptxtduplicateTF1-threaded1"
}
]
}
},
{
"txtFrameArr": {
"CEPTextFrame2": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "NET_CONTENT_STATEMENT",
"IS": "1",
"id": "CE-NET_CONTENT_STATEMENT-1_2_1_1"
}
],
"mapCordinates": [
{
"top": 22.599609375,
"left": 740.23886611805,
"width": 22.9008135909007,
"height": 33.8291015625
},
{
"top": -6.76171875,
"left": 744.522124684196,
"width": 15.4768987533043,
"height": 9.2734375
},
{
"top": -14.33203125,
"left": 740.448785213874,
"width": 22.2500000017299,
"height": 6.88811848711339
}
],
"duplicate": 0,
"content": "4단계 9-14 kg",
"txtframe_name": "ceptxtCE-NET_CONTENT_STATEMENT-1_2_1_1-threaded2"
}
]
}
},
{
"txtFrameArr": {
"CEPTextFrame3": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "USAGE_INSTRUCTIONS",
"IS": "4",
"id": "CE-USAGE_INSTRUCTIONS-4_2_1_1"
}
],
"mapCordinates": [
{
"top": 215.9384765625,
"left": -639.0234375,
"width": 29.1552734375,
"height": 55.2314453125
}
],
"duplicate": 0,
"content": "위로 당기며 입혀줍니다",
"txtframe_name": "ceptxtCE-USAGE_INSTRUCTIONS-4_2_1_1-3"
}
]
}
}
]
Supposed to be wanted like this by removing "txtFrameArr"
[
{
"CEPDuplicateTextFrame1": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "999",
"name": "MARKETING_CLAIM",
"IS": "1",
"id": "CE-MARKETING_CLAIM-1_999_1_1"
}
],
"mapCordinates": [
{
"top": 468.645102009805,
"left": 1053.47265625,
"width": 30.8357511971699,
"height": 132.963461384805
},
{
"top": 513.29412308266,
"left": 1067.05751323149,
"width": 91.1260246588899,
"height": 233.24627152016
}
],
"duplicate": 1,
"content": "baby-dry pants",
"txtframe_name": "ceptxtduplicateTF1-threaded1"
}
]
},
{
"CEPTextFrame2": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "NET_CONTENT_STATEMENT",
"IS": "1",
"id": "CE-NET_CONTENT_STATEMENT-1_2_1_1"
}
],
"mapCordinates": [
{
"top": 22.599609375,
"left": 740.23886611805,
"width": 22.9008135909007,
"height": 33.8291015625
},
{
"top": -6.76171875,
"left": 744.522124684196,
"width": 15.4768987533043,
"height": 9.2734375
},
{
"top": -14.33203125,
"left": 740.448785213874,
"width": 22.2500000017299,
"height": 6.88811848711339
}
],
"duplicate": 0,
"content": "4단계 9-14 kg",
"txtframe_name": "ceptxtCE-NET_CONTENT_STATEMENT-1_2_1_1-threaded2"
}
]
},
{
"CEPTextFrame3": [
{
"marked": 1,
"id_elems": [
{
"PS": "1",
"OS": "1",
"LS": "2",
"name": "USAGE_INSTRUCTIONS",
"IS": "4",
"id": "CE-USAGE_INSTRUCTIONS-4_2_1_1"
}
],
"mapCordinates": [
{
"top": 215.9384765625,
"left": -639.0234375,
"width": 29.1552734375,
"height": 55.2314453125
}
],
"duplicate": 0,
"content": "위로 당기며 입혀줍니다",
"txtframe_name": "ceptxtCE-USAGE_INSTRUCTIONS-4_2_1_1-3"
}
]
}
]
Supposed to be wanted like this by removing "txtFrameArr"
I need to remove {"txtFrameArr"} but the key,value needs to be retain
Is there any specific function in Java Spring for this solution?
Supposed to be wanted like this by removing "txtFrameArr"
Is there any specific function in Java Spring for this solution?
If I get correctly what you mean - you could use JSON Path:
get all values of your objects txtFrameArr key.
Create new json array with extracted values:
For first step - you could use com.jayway.jsonpath:json-path:*, for second step - either GSON or Jackson
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
Code will look like this:
List<Map<String, Object>> frames = JsonPath.parse(json).read("$[*].txtFrameArr");
String newJson = new Gson().toJson(frames);
System.out.println("object: " + newJson);
Related
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"
}
}
I have a JSON
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"card": {
"expiryMonth": "1",
"expiryYear": "2033"
},
"order": {
"amount": 1
}
}
}
}
And I want to remove the card info of it like this:
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"order": {
"amount": 1
}
}
}
}
How can I do this with Apache velocity?
What works is:
#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.replaceAll("2033","2055"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}
This gives me
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"card": {
"expiryMonth": "1",
"expiryYear": "2050"
},
"order": {
"amount": 1
}
}
}
}
But now I want to remove the card part but it does not work:
#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.delete("$.Payload.body.card"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}
what am I doing wrong?
Main goal is transform a mapping template in API Gateway for a webhook. The webhook contains to many information and we want to remove some part of the JSON POST call.
Try using the below
#set($dummy=$content.Payload.remove("card"))
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"
}
]
}
]
}
]
}
[
{
"id": "1234",
"price": 1000,
"categories": [
"Fashion/Shirt"
],
"category_id": [
"1234564"
],
"gender": "Male",
"brand": "Wonder",
"fashion_composition": [
{
"material": "silk",
"percentage": 78
},
{
"material": "Cotton",
"percentage": 22
}
],
"fashion_season": {
"season": "continuity",
"year_season": 2014
},
"fashion_size": {
"size_type": "US",
"size_description_1": "size",
"size_1_manufacturer": "90",
"size_description_2": "top",
"size_2_manufacturer": "C",
"size_1": [
"90"
],
"size_2": [
"C"
]
}
},
{
"id": "5678",
"price": 1000,
"categories": [
"Fashion/Skirt"
],
"category_id": [
"1234564"
],
"gender": "Female",
"brand": "Wonder",
"fashion_composition": [
{
"material": "silk",
"percentage": 78
},
{
"material": "Cotton",
"percentage": 22
}
],
"fashion_season": {
"season": "continuity",
"year_season": 2014
},
"fashion_size": {
"size_type": "US",
"size_description_1": "size",
"size_1_manufacturer": "90",
"size_description_2": "top",
"size_2_manufacturer": "C",
"size_1": [
"90"
],
"size_2": [
"C"
]
}
}
]
Am using gson-2.2.4.jar
I'm guessing you mean by not writing your own bean for GSON to deserialize to. GSON provides support by having certain JSON types that you can make use of:
Gson gson = new Gson();
final JsonArray jsonElements = gson.fromJson(JSON, JsonArray.class);
I have the following JSON object below and I was wondering how to fetch the results of MediaUrl. I found a lot of tutorials on how to parse an array or an object but I can't find anything on how to loop an array inside of an object.
{
"d": {
"results": [
{
"__metadata": {
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=0&$top=1",
"type": "ImageResult"
},
"ID": "17866c98-31f2-4488-9512-e47785301117",
"Title": "acanthurus achilles acanthuridae poissons chirurgiens",
"MediaUrl": "http://www.aquariophilie62.fr/images/poissons-recifal/thumbs/Acanthurus-achilles-.jpg",
"SourceUrl": "http://www.aquariophilie62.fr/fiches-poissons-recifaux/",
"DisplayUrl": "www.aquariophilie62.fr/fiches-poissons-recifaux",
"Width": "100",
"Height": "100",
"FileSize": "19633",
"ContentType": "image/jpeg",
"Thumbnail": {
"__metadata": {
"type": "Bing.Thumbnail"
},
"MediaUrl": "http://ts2.mm.bing.net/th?id=H.4820238373750001&pid=15.1",
"ContentType": "image/jpg",
"Width": "100",
"Height": "100",
"FileSize": "2509"
}
},
{
"__metadata": {
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=1&$top=1",
"type": "ImageResult"
},
"ID": "e2a96fba-f8b2-4212-8477-1bf36131d61c",
"Title": "100x100px-LS-8c425bd7_47876-000676_Acanthurus_achilles.jpg",
"MediaUrl": "http://cdn.saltwaterfish.com/8/8c/100x100px-LS-8c425bd7_47876-000676_Acanthurus_achilles.jpg",
"SourceUrl": "http://forums.saltwaterfish.com/t/346818/humu-humu-trigger-shrimps",
"DisplayUrl": "forums.saltwaterfish.com/t/346818/humu-humu-trigger-shrimps",
"Width": "100",
"Height": "100",
"FileSize": "8185",
"ContentType": "image/jpeg",
"Thumbnail": {
"__metadata": {
"type": "Bing.Thumbnail"
},
"MediaUrl": "http://ts2.mm.bing.net/th?id=H.4781768365638869&pid=15.1",
"ContentType": "image/jpg",
"Width": "100",
"Height": "100",
"FileSize": "2234"
}
},
{
"__metadata": {
"uri": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=2&$top=1",
"type": "ImageResult"
},
"ID": "11d79168-8fd1-49d9-9d06-c54208cecf28",
"Title": "acanthurus bahianus acanthuridae poissons chirurgiens",
"MediaUrl": "http://www.aquariophilie62.fr/images/poissons-recifal/thumbs/Acanthurus-bahianus-.jpg",
"SourceUrl": "http://www.aquariophilie62.fr/fiches-poissons-recifaux/",
"DisplayUrl": "www.aquariophilie62.fr/fiches-poissons-recifaux",
"Width": "100",
"Height": "100",
"FileSize": "19105",
"ContentType": "image/jpeg",
"Thumbnail": {
"__metadata": {
"type": "Bing.Thumbnail"
},
"MediaUrl": "http://ts2.mm.bing.net/th?id=H.4820238373749985&pid=15.1",
"ContentType": "image/jpg",
"Width": "100",
"Height": "100",
"FileSize": "2373"
}
}
],
"__next": "https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/Image?Query='Acanthurus%20achilles'&Adult='Strict'&ImageFilters='Size:Small+Aspect:Square'&Market='en-US'&$skip=3&$top=3"
}
}
Assuming obj is a JSONObject obtained by parsing the posted String
"results" is the key to a JSONArray that you get from your object using
JSONArray results = obj.getJSONObject("d").getJSONArray("results")
JSONArray has get*(int index) for each type of data you can encounter.
In your case, "results" is an array of JSONObject. You can loop through it using :
for (int i = 0; i < results.length(); i++) {
JSONObject oneResult = results.getJSONObject(i);
}
Once you have the JSONObject, the MediaUrl is just one getString("MediaUrl") away ...