I have this json string:
I want to extract all the ids that are after the node of number:"0","1","2"...etc.
I have succeeded to get a single id by using jsonpath: $.response.data.0.id and got "15124".
but i'm looking for a jsonpath that will extract all the ids in the Json String.
in other words this is the expexted output: 15124,13498,14296,13364,14060,13672.
This is the Json String i have:
{
"response": {
"code": 200,
"msg": "Success",
"data": {
"0": {
"id": "15124",
"name": " yoav (yoavshaki#yahoo.com) - 301519506662355",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 3,
"facebook_code": 70
}
},
"1": {
"id": "13498",
"name": "(Not in used) Daniel - 30138444",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"2": {
"id": "14296",
"name": "Daniel - ComeOn (bingocafe#walla.com - 1375713835981039)",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"3": {
"id": "13364",
"name": "Erez - 116060088528093",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
},
"4": {
"id": "14060",
"name": "Erez - NordicBet (gianniciano82#gmail.com - 105134566315107)",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 139,
"code": "IST",
"region": "Asia",
"locality": "Jerusalem",
"offset": 3,
"facebook_code": 70
}
},
"5": {
"id": "13672",
"name": "Erez - alon.dan - 1378526859026272",
"network_id": 1,
"network_type": "Facebook",
"currency": "USD",
"currency_info": {
"prefix": "$",
"postfix": "",
"name": "US Dollars"
},
"timezone": {
"id": 92,
"code": "PST",
"region": "America",
"locality": "Los_Angeles",
"offset": -7,
"facebook_code": 1
}
}
}
}
}
Thanks for all the helpers!
GSON library is a good option to convert java object to json string and vise versa.
for converting json to java object use: fromJson(String, Class)
for converting java object to json string use: toJson(Object)
Here is the sample code using [Gson#fromJson()] to convert JSON string into java Map.
Find more examples...
Sample code:
BufferedReader reader = new BufferedReader(new FileReader(new File("resources/json.txt")));
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Map<String, Object>>>() {}.getType();
Map<String, Map<String, Object>> map = gson.fromJson(reader, type);
Map<String, Map<String, Object>> innerMap = (Map<String, Map<String, Object>>) map.get("response").get("data");
for (String key : innerMap.keySet()) {
System.out.println("key:" + key + " id:" + innerMap.get(key).get("id"));
}
output:
key:0 id:15124
key:1 id:13498
key:2 id:14296
key:3 id:13364
key:4 id:14060
key:5 id:13672
Thanks Syam S. for your answer.
$.response.data.*.id indeed work!
Related
Underneath is a JSON file where it used an OCR from a pdf file table containing data and gets the JSON below. I want the contents of "DATE", "SUM", "MULTIPLE" from this JSON and convert to JAVA so then I can be able to store it to database. I've heard Jackson or JSON validator would do the trick, but in this JSON format, how can I approach this? For instance, you'll see "content": "DATE", how can I convert "DATE" into JAVA object, and then later store to a DataBase?
{"metadata": [],
"pages": [
{
"margins": {
"top": -1,
"left": -1,
"bottom": -1,
"right": -1
},
"box": {
"l": 0,
"t": 0,
"w": 738,
"h": 952
},
"rotation": {
"degrees": "0",
"origin": {
"x": "369",
"y": "476"
},
"translation": {
"x": "0",
"y": "0"
}
},
"pageNumber": 1,
"elements": [
{
"id": 519,
"type": "paragraph",
"properties": {
"order": 0
},
"metadata": [],
"box": {
"l": 102.15,
"t": 118.92,
"w": 107.97,
"h": 106.68
},
"content": [
{
"id": 8,
"type": "line",
"properties": {
"order": 0,
"cr": 210.12,
"cl": 76.43
},
"metadata": [],
"box": {
"l": 168.78,
"t": 118.92,
"w": 41.35,
"h": 12.61
},
"content": [
{
"id": 7,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 168.78,
"t": 118.92,
"w": 41.35,
"h": 12.61
},
"content": "**DATE:**",
"font": 1
}
]
},
{
"id": 18,
"type": "line",
"properties": {
"order": 1,
"cr": 210.12,
"cl": 76.43
},
"metadata": [],
"box": {
"l": 102.15,
"t": 150.24,
"w": 104.5,
"h": 12.54
},
"content": [
{
"id": 16,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 102.15,
"t": 150.24,
"w": 46.49,
"h": 12.28
},
"content": "**SALES**",
"font": 1
},
{
"id": 17,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 152.34,
"t": 150.32,
"w": 54.31,
"h": 12.46
},
"content": "**ORDER:**",
"font": 1
}
]
}
There are many JSON parsers for Java:
Google GSON
Gson gson=new Gson();
MyObject=gson.fromJson(jsonString, MyObject.class);
javax.json
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonObject jsonObject = jsonReader.readObject();
int myInt = jsonObject.getInt("myInt"); // or getString(), getJsonObject() etc.
JSON-Simple (similar usage to javax.json)
org.json: see this StackOverflow answer
and many others to choose from...
I have an arraylist:
{
"response": [
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "0026",
"name": "A name "
},
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "0027",
"name": "A name "
},
]
}
and I have a second arraylist which contains details for each bank. The list is this:
"details": [
{
"bankCode": "010",
"bankDescription": "BANK",
},
{
"bankCode": "011",
"bankDescription": "NATIONAL",
},
{
"bankCode": "012",
"bankDescription": "ALPHA",
}
]
}
Now I want to loop through the first list and put the bankDescription field in each object of the list according to the bankCode and the bankDescription of the second list. I made a stream for the first arrayList:
firtsList.stream().forEach(a -> {
a.put();
});
My guess is that somehow to loop through the second list but I don't know an efficient way. My output must be something like this:
{
"response": [
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "010",
"bankDesc: "BANK"
"name": "A name "
},
{
"number": "229425941 ",
"CURRENCY": "EUR",
"account": "0026.5501.90.0490520505",
"branch": "0154",
"product": "geia",
"service": "0026.0002.62.0300162968",
"amount": 20000,
"expDate": "2019-09-20",
"bank": "011",
"bankDesc: "NATIONAL"
"name": "A name "
},
]
}
secondList.stream().filter(bic -> firstList.stream()
.anyMatch(c -> StringUtils.substring((String) c.get("bankDesc"), 1, 3)
.equals(bic.get("bankCode"))));
Have a model/dto to simplify the process. You can use objectMapper to map Json directly into POJO.
Assuming you disregard all advice and proceed with Json/Map, here's how you can proceed:
//Build a dictionary from the data
Map<String, String> bankCodeToDescriptionMap = secondList.stream().collect(Collectors.toMap(v -> v.get("bankCode"), v -> v.get("bankDescription")));
firstList.stream().forEach(a -> {
a.put("bankDescription", bankCodeToDescriptionMap.get(v.get("bankCode")));
});
Do not iterate over the list for each value. It will be too slow
I have a requirement to use flattened structure JSON, for example below hierarchical Json:
{
"employees": [
{
"employee1": {
"employeeId": 123,
"name": "ABC",
"type": "permanent",
"address": {
"street": "",
"city": "",
"zipcode": 123456
},
"phoneNumbers": [
123456,
987654
],
"designation": "Manager",
"properties": {
"age": "29 years",
"joiningDate": "17-may-2017",
"salary": "1000 USD"
}
}
},
{
"employee2": {
"employeeId": 123,
"name": "XYZ",
"type": "parttime",
"address": {
"street": "",
"city": "",
"zipcode": 345645
},
"phoneNumbers": [
345332,
675444
],
"designation": "Contractor",
"properties": {
"age": "35 years",
"joiningatDate": "17-june-2015",
"salary": "700 USD"
}
}
}
]
}
**Could be represented as flat structure Json as below(generated this using json-flattener):**
{
"employees[0].employee1.address.zipcode": 123456,
"employees[0].employee1.address.city": "",
"employees[0].employee1.address.street": "",
"employees[0].employee1.name": "ABC",
"employees[0].employee1.employeeId": 123,
"employees[0].employee1.designation": "Manager",
"employees[0].employee1.type": "permanent",
"employees[0].employee1.phoneNumbers[0]": 123456,
"employees[0].employee1.phoneNumbers[1]": 987654,
"employees[0].employee1.properties.joiningDate": "17-may-2017",
"employees[0].employee1.properties.salary": "1000 USD",
"employees[0].employee1.properties.age": "29 years",
"employees[1].employee2.address.zipcode": 345645,
"employees[1].employee2.address.city": "",
"employees[1].employee2.address.street": "",
"employees[1].employee2.name": "XYZ",
"employees[1].employee2.employeeId": 123,
"employees[1].employee2.designation": "Contractor",
"employees[1].employee2.type": "parttime",
"employees[1].employee2.phoneNumbers[0]": 345332,
"employees[1].employee2.phoneNumbers[1]": 675444,
"employees[1].employee2.properties.joiningDate": "17-june-2015",
"employees[1].employee2.properties.salary": "700 USD",
"employees[1].employee2.properties.age": "35 years"
}
My problem is if my service receives flattened Json like above, how to convert it to java domain objects automatically like:
class Employee{
Address address;
Properties property;
}
Does any Java Json parser supports this automatic conversion or i will have to implement own parsing logic splitting the keys based on dot in the keys?
Thanks in Advance.
You can take a look at json-flattener.
It does what you want, and even more.
The usage is simple as this:
String json = "{ \"a\" : { \"b\" : 1, \"c\": null, \"d\": [false, true] }, \"e\": \"f\", \"g\":2.3 }";
String jsonStr = JsonFlattener.flatten(json);
System.out.println(jsonStr);
// Output: {"a.b":1,"a.c":null,"a.d[0]":false,"a.d[1]":true,"e":"f","g":2.3}
Currently I'm using rest-assured to hit an endpoint and in turn store any JSON data matches via an ArrayList and HashMap.
I can see that I'm receiving a response back, but how do I loop over an ArrayList when it contains an internal HashMap?
As you can see from the JSON data below, I'm expecting to output all values / matches stored within the ArrayList.
My Code:
public void apiTest() {
String position = "Attacker";
String role = "PLAYER";
Response response = given()
.spec(footballCompetitions_requestSpecification)
.when().get(EndPoint.TEAMS + EndPoint.SQUAD);
ArrayList<Map<String, ?>> allPlayers = response.path
("squad.findAll { it.role == '%s' }.findAll { it.position == '%s' }", position, role);
Example JSON Data:
{
"id": 66,
"area": {
"id": 2072,
"name": "England"
},
"activeCompetitions": [
{
"id": 2021,
"area": {
"id": 2072,
"name": "England"
},
"name": "Premier League",
"code": "PL",
"plan": "TIER_ONE",
"lastUpdated": "2019-01-03T23:39:45Z"
},
{
"id": 2001,
"area": {
"id": 2077,
"name": "Europe"
},
"name": "UEFA Champions League",
"code": "CL",
"plan": "TIER_ONE",
"lastUpdated": "2018-12-13T18:55:02Z"
}
],
"name": "Manchester United FC",
"shortName": "Man United",
"tla": "MNU",
"crestUrl": "http://upload.wikimedia.org/wikipedia/de/d/da/Manchester_United_FC.svg",
"address": "Sir Matt Busby Way Manchester M16 0RA",
"phone": "+44 (0161) 8688000",
"website": "http://www.manutd.com",
"email": "enquiries#manutd.co.uk",
"founded": 1878,
"clubColors": "Red / White",
"venue": "Old Trafford",
"squad": [
{
"id": 3188,
"name": "David De Gea",
"position": "Goalkeeper",
"dateOfBirth": "1990-11-07T00:00:00Z",
"countryOfBirth": "Spain",
"nationality": "Spain",
"shirtNumber": 1,
"role": "PLAYER"
},
{
"id": 3331,
"name": "Marcus Rashford",
"position": "Attacker",
"dateOfBirth": "1997-10-31T00:00:00Z",
"countryOfBirth": "England",
"nationality": "England",
"shirtNumber": 10,
"role": "PLAYER"
},
{
"id": 3372,
"name": "Anthony Martial",
"position": "Attacker",
"dateOfBirth": "1995-12-05T00:00:00Z",
"countryOfBirth": "France",
"nationality": "France",
"shirtNumber": 11,
"role": "PLAYER"
},
{
"id": 3662,
"name": "Romelu Lukaku",
"position": "Attacker",
"dateOfBirth": "1993-05-13T00:00:00Z",
"countryOfBirth": "Belgium",
"nationality": "Belgium",
"shirtNumber": 9,
"role": "PLAYER"
},
If you are using Java 8 or higher, then just iterate over the List first and over the Map in a nested iteration using lambda expressions:
public static void main(String[] args) {
List<Map<String, ?>> jsonList = new ArrayList<>();
Map<String, String> mapOne = new HashMap<String, String>();
mapOne.put("id", String.valueOf(66));
mapOne.put("area", "Some Area");
jsonList.add(mapOne);
jsonList.forEach(map -> {
map.forEach((key, value) -> {
System.out.println(key + ": " + value);
});
});
}
The thing you have to take care of is the String representation of value, since it is a Map<String, ?> in your code and a Map<String, String> in my example, what may lead to problems if you directly use the above example's System.out.println(key + ": " + value);.
First iterate over the list, then iterate over the Map:
for (Map<String, ?> map : allPlayers) {
for (String key : map.keySet()) {
Object player = map.get(key);
// ...
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Here is some JSON output:
{
"model": "model/xxx",
"source_status": true,
"objective_field": "000007",
"locale": "en-US",
"credits": 0.01,
"query_string": "",
"private": true,
"subscription": true,
"updated": "2014-01-01T05:15:47.560445",
"created": "2014-01-01T05:15:47.560417",
"number_of_models": 1,
"description": "",
"name": "Prediction for Category",
"prediction_path": {
"path": [],
"next_predicates": [
{
"value": 1.12945,
"field": "00000e",
"count": 6483,
"operator": ">"
},
{
"value": 1.12945,
"field": "00000e",
"count": 3517,
"operator": "<="
}
],
"bad_fields": [],
"confidence": 0.32562,
"unknown_fields": [],
"objective_summary": {
"categories": [
[
"Whale",
1626
],
[
"Minnow",
1953
],
[
"Fish",
3073
],
[
"Free",
3348
]
]
}
},
"fields": {
"00000e": {
"column_number": 14,
"optype": "numeric",
"datatype": "double",
"order": 5,
"preferred": true,
"prefix": "$",
"name": "Monthly Spend"
},
"000007": {
"optype": "categorical",
"order": 3,
"description": "",
"name": "Category",
"term_analysis": {
"enabled": true
},
"label": "",
"column_number": 7,
"datatype": "string",
"preferred": true
}
},
"tags": [],
"shared": false,
"objective_field_name": "Category",
"status": {
"progress": 1,
"message": "The prediction has been created",
"elapsed": 0.028,
"code": 5
},
"resource": "prediction/52c3a483e61ab00496000000",
"objective_fields": [
"000007"
],
"model_status": true,
"prediction": {
"000007": "Free"
},
"input_data": {},
"dataset_status": true,
"tlp": 1,
"confidence": 0.3245,
"code": 201,
"missing_strategy": 0,
"category": 0,
"source": "source/xxxxx",
"dataset": "dataset/xxxxx",
"model_type": 0,
"output": "Free"
}
How can I get the Confidence value inside the prediction_path ?
Parse the json as below
JSONObject jb = new JSONObject("my string");
JSONObject jb1= jb.getJSONObject("prediction_path");
String confidence = jb1.getDouble("confidence");
Your JSON
{ // JSONObject node
"model": "model/xxx",
prediction_path": { // json object prediction path
"confidence": 0.32562,
http://developer.android.com/reference/org/json/JSONObject.html
String line = "";
String NL = System.getProperty("line.separator");
in = new BufferedReader(new InputStreamReader(YourHttpResponse
.getEntity().getContent()));
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
json = sb.toString();
and then:
JSONObject _jasonObj = new JSONObject(json);
JSONObject _json = new JSONObject((_jasonObj.getString("prediction_path"));
String _confidence = _json.get("confidence");