I am using jsonpath to filter.
Json(Dummy json just to explain) source String, which is basically a list of Operating systems and details of its programs etc. In this example, the OS whose id = 1403 is a windows 10 OS and has 2 features acchritecture and browser. There are more details to the browser feature as shown in json
[
{
"id": 1403,
"os": "window 10",
"features": [
{
"id": 1356,
"name": "architecture",
"value": [
{
"id": 1308,
"feature": [
{
"id": 1262,
"key": "name",
"value": "amd64"
}
]
}
],
"category": "cat1"
},
{
"id": 1357,
"name": "browser",
"value": [
{
"id": 1309,
"feature": [
{
"id": 1263,
"key": "name",
"value": "Firefox"
},
{
"id": 1265,
"key": "version",
"value": "187"
}
]
}
],
"category": "cat2"
}
]
},
{
"id": 2804,
"os": "window 7",
"features": [
{
"id": 2764,
"name": "architecture",
"value": [
{
"id": 2719,
"feature": [
{
"id": 2679,
"key": "name",
"value": "amd64"
}
]
}
],
"category": "cat1"
},
{
"id": 2765,
"name": "browser",
"value": [
{
"id": 2722,
"feature": [
{
"id": 2685,
"key": "name",
"value": "Chrome"
},
{
"id": 2684,
"key": "version",
"value": "87.0.4280.88"
}
]
}
],
"category": "cat2"
}
]
}
]
I want to be able to filter the json such that
features[*].name == 'browser' and features[*].value[*].feature[*].value == 'chrome'
What will be the JsonPath string that can help me achieve above query? The above query uses similar syntax used by JsonPath string but doesn't do the job. Its just to explain.
There is another example here gets Movie Title Given 'Starring' field
And would like to get the full OS json that fulfils this condition. In this case a array of OS which contains only one OS i.e. with id= 2804
[
{
"id": "2804",
...
}
]
I am stuck much before what aim to achieve. Here is my code to get all the OS that have "name=browser". I get the array but it only contains value[] items. I want it get the full json. It returns object with IDs- 1357, 2765.
List<Map<String, Object>> expensive = JsonPath.parse(jsonDataSourceString)
.read("$[*].features[*].[?(#.name == 'browser')]");
To get the outer array you need to use the filter like $[?(...)]
For your current use case, we need to use nested array filters. There is an open issue in JsonPath for filter on children level. (Refer here).
Luckily, there is a workaround suggested to use contains over here.
we can use the below expression to filter:
List<Object> expensive = JsonPath.parse(jsonDataSourceString)
.read("$[?(#.features[?(#.name == 'browser')].value[*].feature[*].value contains 'Chrome')]");
Prints the below output
{id=2804, os=window 7, features=[{"id":2764,"name":"architecture","value":[{"id":2719,"feature":[{"id":2679,"key":"name","value":"amd64"}]}],"category":"cat1"},{"id":2765,"name":"browser","value":[{"id":2722,"feature":[{"id":2685,"key":"name","value":"Chrome"},{"id":2684,"key":"version","value":"87.0.4280.88"}]}],"category":"cat2"}]}
This is my sample Json
{
"State": {
"version": "1",
"SName": "Test",
"shippingDetails": {
"Address1": "AP",
"ZipCode": "1236"
},
"Directions": {
"routes": [
{
"taxAmount": "0.0",
"Quantity": "5",
"bounds": {
"SerialVersion": [
{
"text": "1.7 km",
"value": "1729",
"time": "02633"
},
{
"text": "1.9 km",
"value": "1829",
"time": "02353"
},
{
"text": "17 km",
"value": "1059",
"time": "02133"
}
]
}
}
]
}
}
}
I want to update SName, ZipCode,taxAmount,Quantity, and text[1] values
are there any way to do this. I am taking JSON in a file and update tags are taking into HashMap
JSONObject jsonObject = new JSONObject("Your_JSON_String");
JSONObject jsonObjectForState = jsonObject.getJSONObject(“State”);
jsonObjectForState.put("Sname", "New_Value_Here");
put(...) will replace the current value with new value. Similarly, you can update the other values. Once you are done, you can convert it back using:
jsonObject.toString();
And write it back to the file.
I have the following json, how can i convert it to arrayList?
"data": [{
"label": "Venezuela",
"value": "290"
}, {
"label": "Saudi",
"value": "260"
}, {
"label": "Canada",
"value": "180"
}, {
"label": "Iran",
"value": "140"
}, {
"label": "Russia",
"value": "115"
}, {
"label": "UAE",
"value": "100"
}, {
"label": "US",
"value": "30"
}, {
"label": "China",
"value": "30"
}]
}
You need to convert it manually iterate each json and assign it to an object then you can add it to a collection.
or even you can use Gson Library take a look at the following, gson can map your attribute into Java Object.
JsonElement yourJson = mapping.get("data");
Type listType = new TypeToken<List<YourModel>>() {}.getType(); //YourModel should contains the same attributes which the json have (Exactly)
List<String> yourList = new Gson().fromJson(yourJson, listType);
I'm trying to create POJOs for the following JSON from the API at https://developers.google.com/qpx-express/v1/trips/search
When I copy & paste this into http://www.jsonschema2pojo.org/ I get an error at line 5 "requestId": string, saying "There's a problem: Unrecognized token 'string': was expecting ('true', 'false' or 'null') (line 5, column 24)"
If I change string to an actual example string it works fine. Can this json to pojo converter not handle JSON in this format? It just seems a bit tedious to have to change all the data types to samples instead.
{
"kind": "qpxExpress#tripsSearch",
"trips": {
"kind": "qpxexpress#tripOptions",
"requestId": string,
"data": {
"kind": "qpxexpress#data",
"airport": [
{
"kind": "qpxexpress#airportData",
"code": string,
"city": string,
"name": string
}
],
"city": [
{
"kind": "qpxexpress#cityData",
"code": string,
"country": string,
"name": string
}
],
"aircraft": [
{
"kind": "qpxexpress#aircraftData",
"code": string,
"name": string
}
],
"tax": [
{
"kind": "qpxexpress#taxData",
"id": string,
"name": string
}
],
"carrier": [
{
"kind": "qpxexpress#carrierData",
"code": string,
"name": string
}
]
},
"tripOption": [
{
"kind": "qpxexpress#tripOption",
"saleTotal": string,
"id": string,
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": integer,
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": integer,
"flight": {
"carrier": string,
"number": string
},
"id": string,
"cabin": string,
"bookingCode": string,
"bookingCodeCount": integer,
"marriedSegmentGroup": string,
"subjectToGovernmentApproval": boolean,
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": string,
"aircraft": string,
"arrivalTime": string,
"departureTime": string,
"origin": string,
"destination": string,
"originTerminal": string,
"destinationTerminal": string,
"duration": integer,
"operatingDisclosure": string,
"onTimePerformance": integer,
"mileage": integer,
"meal": string,
"secure": boolean,
"connectionDuration": integer,
"changePlane": boolean
}
],
"connectionDuration": integer
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": string,
"carrier": string,
"origin": string,
"destination": string,
"basisCode": string,
"private": boolean
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": string,
"segmentId": string,
"freeBaggageOption": [
{
"kind": "qpxexpress#freeBaggageAllowance",
"bagDescriptor": [
{
"kind": "qpxexpress#bagDescriptor",
"commercialName": string,
"count": integer,
"description": [
string
],
"subcode": string
}
],
"kilos": integer,
"kilosPerPiece": integer,
"pieces": integer,
"pounds": integer
}
]
}
],
"baseFareTotal": string,
"saleFareTotal": string,
"saleTaxTotal": string,
"saleTotal": string,
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": integer,
"childCount": integer,
"infantInLapCount": integer,
"infantInSeatCount": integer,
"seniorCount": integer
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": string,
"chargeType": string,
"code": string,
"country": string,
"salePrice": string
}
],
"fareCalculation": string,
"latestTicketingTime": string,
"ptc": string,
"refundable": boolean
}
]
}
]
}
}
Valid json requires string values to be in quotes. So quoting the key but not the value like-> "id" : string, does not make much sense. Read carefully "In the request body, supply data with the following structure" You are not supplying any data
Well, the example JSON code on the page uses data types in quotes, so "string" instead of string. You could do just that:
(warning! the following example is nonsense if you want other types than strings!)
{
"kind": "qpxExpress#tripsSearch",
"trips": {
"kind": "qpxexpress#tripOptions",
"requestId": "string",
"data": {
"kind": "qpxexpress#data",
"airport": [
{
"kind": "qpxexpress#airportData",
"code": "string",
"city": "string",
"name": "string"
}
],
"city": [
{
"kind": "qpxexpress#cityData",
"code": "string",
"country": "string",
"name": "string"
}
],
"aircraft": [
{
"kind": "qpxexpress#aircraftData",
"code": "string",
"name": "string"
}
],
"tax": [
{
"kind": "qpxexpress#taxData",
"id": "string",
"name": "string"
}
],
"carrier": [
{
"kind": "qpxexpress#carrierData",
"code": "string",
"name": "string"
}
]
},
"tripOption": [
{
"kind": "qpxexpress#tripOption",
"saleTotal": "string",
"id": "string",
"slice": [
{
"kind": "qpxexpress#sliceInfo",
"duration": "integer",
"segment": [
{
"kind": "qpxexpress#segmentInfo",
"duration": "integer",
"flight": {
"carrier": "string",
"number": "string"
},
"id": "string",
"cabin": "string",
"bookingCode": "string",
"bookingCodeCount": "integer",
"marriedSegmentGroup": "string",
"subjectToGovernmentApproval": "boolean",
"leg": [
{
"kind": "qpxexpress#legInfo",
"id": "string",
"aircraft": "string",
"arrivalTime": "string",
"departureTime": "string",
"origin": "string",
"destination": "string",
"originTerminal": "string",
"destinationTerminal": "string",
"duration": "integer",
"operatingDisclosure": "string",
"onTimePerformance": "integer",
"mileage": "integer",
"meal": "string",
"secure": "boolean",
"connectionDuration": "integer",
"changePlane": "boolean"
}
],
"connectionDuration": "integer"
}
]
}
],
"pricing": [
{
"kind": "qpxexpress#pricingInfo",
"fare": [
{
"kind": "qpxexpress#fareInfo",
"id": "string",
"carrier": "string",
"origin": "string",
"destination": "string",
"basisCode": "string",
"private": "boolean"
}
],
"segmentPricing": [
{
"kind": "qpxexpress#segmentPricing",
"fareId": "string",
"segmentId": "string",
"freeBaggageOption": [
{
"kind": "qpxexpress#freeBaggageAllowance",
"bagDescriptor": [
{
"kind": "qpxexpress#bagDescriptor",
"commercialName": "string",
"count": "integer",
"description": [
"string"
],
"subcode": "string"
}
],
"kilos": "integer",
"kilosPerPiece": "integer",
"pieces": "integer",
"pounds": "integer"
}
]
}
],
"baseFareTotal": "string",
"saleFareTotal": "string",
"saleTaxTotal": "string",
"saleTotal": "string",
"passengers": {
"kind": "qpxexpress#passengerCounts",
"adultCount": "integer",
"childCount": "integer",
"infantInLapCount": "integer",
"infantInSeatCount": "integer",
"seniorCount": "integer"
},
"tax": [
{
"kind": "qpxexpress#taxInfo",
"id": "string",
"chargeType": "string",
"code": "string",
"country": "string",
"salePrice": "string"
}
],
"fareCalculation": "string",
"latestTicketingTime": "string",
"ptc": "string",
"refundable": "boolean"
}
]
}
]
}
}
I have following JSON
"ID": "234AS",
"Name": "SynchronousMate",
"Type": "Node",
"SubType": "SubNode",
"Dynamic": "Yes",
"DisplayName": "Sync",
"Direct": "Yes",
"Category": "IT",
"Properties": {
"Property": [
{
"Name": "A",
"Value": "Anant"
},
{
"Name": "B",
"Value": "Bharat"
},
{
"Name": "C",
"Value": "Cynus"
},
{
"Name": "D",
"Value": "Dynana"
},
{
"Name": "E",
"Value": "Elegant"
},
{
"Name": "Bank",
"Value": "BOB"
},
{
"Name": "ipAddress",
"Value": "101.90.34.12"
},
{
"Name": "siteName",
"Value": "BRS-WDM-PSS-X7A6"
},
{
"Name": "Longitude",
"Value": 0
},
{
"Name": "FullName",
"Value": "network:10.254.0.46"
},
{
"Name": "NumberOfShelves",
"Value": 0
},
{
"Name": "GEOCODE.Latitude",
"Value": 0
}
]
},
"Properties": ""
}
..............................
..............................
How to convert this JSON to like this
{
"ID": "234AS",
"Name": "SynchronousMate",
"Type": "Node",
"SubType": "SubNode",
"Dynamic": "Yes",
"DisplayName": "Sync",
"Direct": "Yes",
"Category": "IT",
"A" : "Anant",
"B" : "Bharat",
"C" : "Cynus",
"D" : "Dynana",
"E" : "Elegant",
"Bank" : "BOB",
"ipAddress" : "101.90.34.12",
"siteName" : "BRS-WDM-PSS-X7A6",
"Longitude" : ""0",
"FullName" : "network:10.254.0.46",
"NumberOfShelves" : 0,
"GEOCODE.Latitude" : 0
},
............................
............................
You can loop over properties and create a Map with key the value of Name and as value the value of Value.
In simple terms, Get/Store the Property JsonArray. Then iterate it putting the
elements of array in Map. And then add the map to the other half of JSON.