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...
Related
I have a JSON output like this:
{
"items": [
{
"id": "1",
"name": "Anna",
"values": [
{
"code": "Latin",
"grade": 1
},
{
"code": "Maths",
"grade": 5
}
]
},
{
"id": "2",
"name": "Mark",
"values": [
{
"code": "Latin",
"grade": 5
},
{
"code": "Maths",
"grade": 5
}
]
}
]
}
I need to get field values for "name": "Anna". I am getting RestAssured Response and would like to use my beans to do that, but I can also use jsonPath() or jsonObject(), but I don't know how. I searched many topics but did not find anything.
I have a JSON file that has the following format:
{
"debugOutput": {
"pathCalculationTime": 0,
"pathTimes": [
0
],
"precalculationTime": 0,
"renderingTime": 0,
"timedOut": true,
"totalTime": 0
},
"error": {
"id": 0,
"message": "PLAN_OK",
"missing": [
"string"
],
"msg": "string",
"noPath": true
},
"plan": {
"date": 0,
"from": {
"lat": 0,
"lon": 0,
"name": "string",
"orig": "string",
"stopCode": "string",
"vertexType": "string"
},
"itineraries": [{
"duration": 0,
"elevationGained": 0,
"elevationLost": 0,
"endTime": 0,
"fare": {},
"legs": [{
"agencyId": "string",
"agencyName": "string",
"agencyTimeZoneOffset": 0,
"agencyUrl": "string",
"alerts": [{}],
"alightRule": "string",
"arrivalDelay": 0,
"boardRule": "string",
"departureDelay": 0,
"distance": 0,
"duration": 0,
"endTime": 0,
"from": {
"lat": 0,
"lon": 0,
"name": "string",
"orig": "string",
"stopCode": "string",
"vertexType": "string"
},
"headsign": "string",
"headway": 0,
"interlineWithPreviousLeg": true,
"intermediateStops": [{
"lat": 0,
"lon": 0,
"name": "string",
"orig": "string",
"stopCode": "string",
"vertexType": "string"
}],
"isNonExactFrequency": true,
"legGeometry": {
"length": 0,
"points": "string"
},
"mode": "WALK",
"pathway": true,
"realTime": true,
"rentedBike": true,
"route": "string",
"routeColor": "string",
"routeId": "string",
"routeLongName": "string",
"routeShortName": "string",
"routeTextColor": "string",
"routeType": 0,
"serviceDate": "string",
"startTime": 0,
"steps": [{
"absoluteDirection": "NORTH",
"alerts": [{}],
"area": true,
"bogusName": true,
"distance": 0,
"elevation": [
"string"
],
"exit": "string",
"lat": 0,
"lon": 0,
"relativeDirection": "DEPART",
"stayOn": true,
"streetName": "string"
}],
"to": {
"lat": 0,
"lon": 0,
"name": "string",
"orig": "string",
"stopCode": "string",
"vertexType": "string"
},
"transitLeg": true,
"tripBlockId": "string",
"tripId": "string",
"tripShortName": "string"
}],
"startTime": 0,
"tooSloped": true,
"transfers": 0,
"transitTime": 0,
"waitingTime": 0,
"walkDistance": 0,
"walkLimitExceeded": true,
"walkTime": 0
}],
"to": {
"lat": 0,
"lon": 0,
"name": "string",
"orig": "string",
"stopCode": "string",
"vertexType": "string"
}
},
"requestParameters": {
"app_id": "string",
"app_key": "string",
"arriveBy": "string",
"date": "string",
"fromPlace": "string",
"maxWalkDistance": "string",
"mode": "string",
"time": "string",
"toPlace": "string"
}
}
As you can see, this JSON has 4 main objects (debugOutput, error, plan and requestParameters). I only want to store into my program the data and objects that are in the object plan avoiding all the data of debugOutput, error and requestParameters.
What my current knowledge allows me to do is create an object (dataJson) that contain all 4 objects of this Json (debugOutput, error, plan and requestParameters) and then execute the following line with Gson library:
dataJson = new Gson().fromJson(jsonfile.json, DataJson.class);
But this requires to create all objects and store information I don not need.
How can I read this Json using Gson library in order to store at once all the plan data on a plan object?
Thanks in advance.
You can parse the whole string and just pick out the property you need, in this case the "plan" object.
JsonObject jsonObject = JsonParser.parseString(thejson).getAsJsonObject();
JsonObject planJson = jsonObject.get("plan").getAsJsonObject();
// Store planJson somewhere
I have this JSON schema. I want to read it line by line and populate all "name" key values to an ArrayList of String until it reaches a key called entity. When it reaches to an entity I want to read the entity value and populate it to a String.After that loop should go through the remains of the schema and read all the "name" and populate to a new ArrayList until it meets another entity and so on...
I've only included a part of the schema it's a valid schema.
{
"entity": "Data",
"xement": [
{
"code": "MA",
"entity": "MH",
"attr": [
{
"name": "Id",
"position": 1
},
{
"name": "mdc",
"position": 4
},
{
"name": "minr",
"position": 10
},
{
"name": "mx",
"position": 11
}
],
"maltr": [
{
"name": "sub",
"position": 13
}
]
},
{
"code": "war",
"entity": "subl",
"attr": [
{
"name": "se",
"position": 1
},
{
"name": "go",
"position": 2
},
{
"name": "re",
"position": 4
},
{
"name": "pr",
"position": 10
},
{
"name": "mxp",
"position": 11
},
{
"name": "rpl",
"position": 45
},
{
"name": "rtr",
"position": 47
},
{
"name": "net",
"position": 55
}
],
"groups": [
{
"entity": "ro",
"grattr": [
{
"name": "rmn",
"position": 5
},
{
"name": "aib",
"position": 6
},
{
"name": "nxr",
"position": 7
},
{
"name": "xer",
"position": 8
},
{
"name": "rog",
"position": 9
},
{
"name": "ccc",
"position": 16
}
]
}
]
}
]
}
What is the best way to do that?
For example, I have a JSON database like this:
[
{
"address": "America",
"location": {
"lng": 106.669633,
"lat": 10.771883
},
"name": "724 (100) Pyrmon"
},
{
"address": "America",
"location": {
"lng": 106.668821,
"lat": 10.767997
},
"name": "900 (100) Glassdor"
},
{
"address": "328A, Đường Lê Hồng Phong, Ward 1, Quận 10, TP.HCM, Việt Nam",
"location": {
"lng": 106.676276,
"lat": 10.763239
},
"name": "Booking Office Of Phuong Trang Buslines"
},
{
"address": "America",
"location": {
"lng": 106.690329,
"lat": 10.768556
},
"name": "Opposite Pyrmont"
},
{
"address": "America",
"location": {
"lng": 106.691759,
"lat": 10.763937
},
"name": "150 Washington"
},
{
"address": "America",
"location": {
"lng": 106.681683,
"lat": 10.76965
},
"name": "596 - 598 Nguyễn Đình Chiểu"
},
{
"address": "America",
"location": {
"lng": 106.690158,
"lat": 10.77509
},
"name": "17 Bà Huyện Thanh Quan"
},
{
"address": "America",
"location": {
"lng": 106.699749,
"lat": 10.774052
},
"name": "101 Jase"
},
{
"address": "America",
"location": {
"lng": 106.682621,
"lat": 10.778547
},
"name": "274 Lost Region"
},
{
"address": "America",
"location": {
"lng": 106.67305,
"lat": 10.765617
},
"name": "66 Office Base"
},
{
"address": "America",
"location": {
"lng": 106.689733,
"lat": 10.763817
},
"name": "118 Yen The"
},
{
"address": "America",
"location": {
"lng": 106.69313,
"lat": 10.765497
},
"name": "112 Glangdie"
},
]
Now I have to make an algorithm (in any programming language, if in Java, it will be good) that will input the latitude, longitude, and the distance D . The output will be JSON data that contain the information of nearby places that have the distance to the input distance smaller than D .
I have researched about this algorithm, especially on Wikipedia, http://en.wikipedia.org/wiki/Geographical_distance#Singularities_and_discontinuity_of_latitude.2Flongitude.
But I cannot find a way to query from the database, if the database is BIG, I have to query each row(or each JSON Object in this case) to compare with the chosen location, that will be a very slow performance.
How can I do this?
You can try to reduce comparisons by filtering locations inside circumscribed square around circle with input radius. In SQL it looks like: WHERE lng>=(:lng - :radius) AND lng<=(:lng + :radius) AND lat>=(:lat - :radius) AND lat<=(:lat + :radius), where :lng, :lat - input coordinates, :radius - input radius in radians.
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!