Read a property from a DBObject [duplicate] - java

This question already has answers here:
MongoDB extracting values from BasicDBObject (Java)
(2 answers)
Closed 5 years ago.
I want to get an JSON array into a var in java. My JSON seems ok but when i try to put a JSOn array into a java array var it does not work.
here the error i get : errororg.json.JSONException: JSONObject["tweets"] is not a JSONArray.
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "test1" );
DBCollection coll = db.getCollection("tweetsCol");
DBCursor cursor = coll.find();
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
JSONObject objjj = new JSONObject(obj);
try{
System.out.println("okok "+objjj); // THE JSON I WILL SHOW YOU
JSONArray jsonMainArr = objjj.getJSONArray("tweets");
}catch(JSONException e){
System.out.println("error"+e);
}}
Here's my data in MongoDB:
{
"_id":"5939bc6676abbe186feb73a5",
"user_request_id":"5941903f37aaa6ec55689e85",
"tweets":[
{
"date":"Wed Jun 07 18:32:57 CDT 2017",
"text":"[Earthview Wonders][Video] No.265: Astronaut Thomas Pesquet completed 6-month #MissionProxima. #Neweyes\u2026 ",
"_id":872597276891398144,
"user":"livearthjp"
},
{
"date":"Wed Jun 07 18:16:56 CDT 2017",
"text":"Astronaut Thomas Pesquet #Thom_astro Shares His #Songs4Space ",
"_id":872593245716467712,
"user":"anasia5mice"
},
{
"date":"Wed Jun 07 15:46:03 CDT 2017",
"text":"Thomas Pesquet: Undocking and landing ",
"_id":872555275387117570,
"user":"GRASSIFREE"
},
{
"date":"Wed Jun 21 17:02:37 CDT 2017",
"text":"#Thom_astro #Space_Station And his colleagues said, 'Pesquet, if you play Baker Street one more time...'",
"_id":877647972430823429,
"user":"kimkemmis"
},
{
"date":"Wed Jun 21 17:01:16 CDT 2017",
"text":"[News] ",
"_id":877647632524394497,
"user":"ArthurC2Pouce"
},
{
"date":"Wed Jun 21 11:28:48 CDT 2017",
"text":"Thomas Pesquet's music is OUT THERE! Cool dude. ",
"_id":877563967178104836,
"user":"tiarudd34"
},
{
"date":"Wed Jun 21 11:10:15 CDT 2017",
"text":"jaime thomas pesquet",
"_id":877559296741048320,
"user":"sosthene_maus"
},
{
"date":"Wed Jun 21 10:23:03 CDT 2017",
"text":"French astronaut Thomas Pesquet took some of the most amazing pictures ever while in spce ",
"_id":877547418606329861,
"user":"raygibbs1"
},
{
"date":"Wed Jun 21 10:23:00 CDT 2017",
"text":"French astronaut Thomas Pesquet shares stunning pictures of Earth: via #AOL",
"_id":877547405180157952,
"user":"raygibbs1"
},
{
"date":"Wed Jun 21 08:46:13 CDT 2017",
"text":"Coll Cambuston like Thomas Pesquet! #thomastro #cardierun",
"_id":877523048546676736,
"user":"CambF974"
},
{
"date":"Wed Jun 21 08:00:06 CDT 2017",
"text":"Thomas Pesquet returned to Earth on 2 June 2017 after completion of his six-months long Proxima mission to the... ",
"_id":877511443775619072,
"user":"rospaceagency"
},
{
"date":"Tue Jun 20 23:50:34 CDT 2017",
"text":"Thomas Pesquet #Thom_astro and Messier 83 #Astronauts #ESA #CNES #NASA ✨👨\u200d🚀✨ ",
"_id":877388248368033794,
"user":"AmirAliBehrooz"
}
],
"query_name":"Pesquet",
"active":"true",
"started_at":"2017_06_08"
}

Now I understand better your question thanks to #Neil Lunn, and following his best advice, you have to understand that you are not dealing here with Json, you can directly do:
MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
DB db = mongoClient.getDB( "test1" );
DBCollection coll = db.getCollection("tweetsCol");
DBCursor cursor = coll.find();
while (cursor.hasNext()) {
BasicDBObject obj = (BasicDBObject) cursor.next();
try{
System.out.println("okok "+obj); // THE JSON I WILL SHOW YOU
BasicDBList jsonMainArr = obj.get("tweets");
}catch(JSONException e){
System.out.println("error"+e);
}}

Related

Pretty print String to JSON format

I execute an AWS command to retrieve the spot price history.
DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest().withEndTime(start)
.withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)").withStartTime(end);
DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);
System.out.println(response.toString());
I obtained the result in json format but I receive it in String like:
{SpotPriceHistory: [{AvailabilityZone: us-east-1d,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1c,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1b,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1a,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.035000,Timestamp: Wed Nov 07 00:18:50 CET 2018}, {AvailabilityZone: us-east-1e,InstanceType: m1.xlarge,ProductDescription: Linux/UNIX,SpotPrice: 0.350000,Timestamp: Thu Sep 20 01:08:39 CEST 2018}]}
my question is: How can I improve the displaying of the results ? like
{
"Timestamp": "2018-09-08T17:07:14.000Z",
"AvailabilityZone": "us-east-1d",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
},
{
"Timestamp": "2018-09-08T17:07:14.000Z",
"AvailabilityZone": "us-east-1c",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
},
{
"Timestamp": "2018-09-08T17:07:14.000Z",
"AvailabilityZone": "us-east-1b",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
},
{
"Timestamp": "2018-09-08T12:32:28.000Z",
"AvailabilityZone": "us-east-1e",
"InstanceType": "p2.16xlarge",
"ProductDescription": "Linux/UNIX",
"SpotPrice": "4.320000"
}
]
}
You're calling the .toString() on the response object, just be careful with this as there is no guarantee that will always be json, as you're seeing above, it's not even valid json as it's missing quotes around the attribute names and values.
One option to get what you want is to call response.getSpotPriceHistory() to get you the array of spot prices, then pass that thru ObjectMapper and write it as a pretty string, like so:
public static void main(String[] args) throws IOException {
AmazonEC2 client = AmazonEC2Client.builder().build();
DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest()
.withEndTime(new Date())
.withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)")
.withStartTime(new Date());
DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);
ObjectMapper mapper = new ObjectMapper();
String asPrettyJSon = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(response.getSpotPriceHistory());
System.out.println(asPrettyJSon);
}
Both represent a json array containing jsonObjects of same structure.
Displaying result will depend on your front implementation not the layaout of your jsonRespense.

How to make a JSON object ordered?

Java file to set and get the values. I have set the boId as the first:
if(etsBuildOrder != null){
buildOrder.setBoId(etsBuildOrder.getBoId());
buildOrder.setName(etsBuildOrder.getName());
buildOrder.setFactory(etsBuildOrder.getFactory());
buildOrder.setStatus(etsBuildOrder.getStatus());
buildOrder.setIssued(etsBuildOrder.getIssued());
buildOrder.setTeam(etsBuildOrder.getTeam());
buildOrder.setType(etsBuildOrder.getType());
buildOrder.setBuildId(etsBuildOrder.getBuildRequestId());
buildOrder.setPartNumber(etsBuildOrder.getPartNumber());
buildOrder.setProductCode(etsBuildOrder.getProductCode());
buildOrder.setSpecialInstructions(etsBuildOrder.getSpecialInstructions());
buildOrder.setBoCreationDate(RestWsUtil.convertDateToString(etsBuildOrder.getCreationDate(), Constants.SIMPLE_DATE_FORMAT_DATE_ONLY));
buildOrder.setBoModifiedDate(RestWsUtil.convertDateToString(etsBuildOrder.getModifiedDate(), Constants.SIMPLE_DATE_FORMAT_DATE_ONLY));
buildOrder.setChangeHistory(etsBuildOrder.getChangeHistory());
}
JSON return format. boId is not located at the beginning of JSON:
{
"name": "TLO9009",
"factory": "L-Slider",
"type": null,
"boCreationDate": "18 Apr 2018",
"boModifiedDate": "18 Apr 2018",
"status": "Pending Approval",
"team": null,
"partNumber": null,
"specialInstructions": "Special Inst",
"changeHistory": "Pending ApprovalWed Apr 18 10:14:06 SGT 2018",
"productCode": null,
"issued": null,
"multifeature": null,
"buildId": 0,
"boId": 141
}
How the JSON should return. I would like the JSON to return like this:
{
"boId": 141
"name": "TLO9009",
"factory": "L-Slider",
"type": null,
"boCreationDate": "18 Apr 2018",
"boModifiedDate": "18 Apr 2018",
"status": "Pending Approval",
"team": null,
"partNumber": null,
"specialInstructions": "Special Inst",
"changeHistory": "Pending ApprovalWed Apr 18 10:14:06 SGT 2018",
"productCode": null,
"issued": null,
"multifeature": null,
"buildId": 0
}
I am new to JAVA and help is much appreciated.Thanks in advance.
That is not possible and not necessary. Both JSON objects are identical, there is not order.
An object is an unordered set of name/value pairs. An object begins with { (left brace) and ends with } (right brace). Each name is followed by : (colon) and the name/value pairs are separated by , (comma).
https://json.org/

How to read a value of a key from JSONstring

Below is the json data:
"headers": [
{
"name": "date",
"value": "Tue, 13 Mar 2018 12:44:55 GMT"
},
{
"name": "content-encoding",
"value": "gzip"
},
{
"name": "last-modified",
"value": "Tue, 06 Mar 2018 20:15:30 GMT"
},
{
"name": "server",
"value": "Apache"
},
{
"name": "vary",
"value": "Accept-Encoding"
},
{
"name": "content-type",
"value": "text/html"
},
{
"name": "status",
"value": "200"
}]
my requirement is to capture date, content-encoding, last-modified, server and status from the above json using java
i am using json-sample-1.1.1 version
can some one please assist how to get these values
Or if you prefer Java 8 you can use it by this way.
JSONParser parser = new JSONParser();
JSONObject jsonObject = (JSONObject)parser.parse("{\"headers\":[{\"name\":\"date\",\"value\":\"Tue, 13 Mar 2018 12:44:55 GMT\"},{\"name\":\"content-encoding\",\"value\":\"gzip\"},{\"name\":\"last-modified\",\"value\":\"Tue, 06 Mar 2018 20:15:30 GMT\"},{\"name\":\"server\",\"value\":\"Apache\"},{\"name\":\"vary\",\"value\":\"Accept-Encoding\"},{\"name\":\"content-type\",\"value\":\"text/html\"},{\"name\":\"status\",\"value\":\"200\"}]}");
JSONArray headers = (JSONArray)jsonObject.get("headers");
headers.forEach( header -> {
JSONObject headerJsonObject = (JSONObject)header;
if(headerJsonObject.get("name").equals("content-encoding")){
System.out.println(headerJsonObject.get("value"));
}
// continue
});
Iterate over the array, compare names, and retrieve it's value
String contentencoding = null;
JSONArray headers = new JSONArray(yourHeadersJsonString);
for ( int n = 0; n < headers.length(); n++ )
{
if ( headers[n].name.equals("content-encoding") )
{
contentencoding = headers[n].value;
break;
}
}
JSONObject jsonObject=new JSONObject(response);
JSONArray jsonArray=jsonObject.getJSONArray("array_name");
then start loop to the size of jsonarray and get the values by using methods like getString("key_name") of JSONObject

how to split 1GB JSON file into multiple JSON object file each

I have a JSON file of size 1GB which contains n number of twitter JSON Object tweets.Now I want to split large JSON file into single JSON Object each. But in large file the JSON chunks are space separated but not comma separated.
INPUT :-
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487360",
"text": "Soo sad right now..",
"filter_level": "medium",
"lang": "en"
}
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487361",
"text": "الضمير صوت هادىء يخبرك بأن احدا ينظر اليك",
"lang": "en"
}
Now my OUTPUT has to be as shown below
OUTPUT: Test1.json
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487360",
"text": "Soo sad right now..",
"filter_level": "medium",
"lang": "en"
}
Test2.json
{
"created_at": "Tue Aug 06 06:01:00 +0000 2013",
"id": 364627145785487360,
"id_str": "364627145785487361",
"text": "الضمير صوت هادىء يخبرك بأن احدا ينظر اليك",
"lang": "en"
}
Can anyone help me out to get the above output JSON files i.e, Test1.json, Test2.json
given number of lines (7000 in this case):
command : jq -c -M '.data[]' | split -l 7000

Mapping a JAVA object with a MongoDB document using jongo

Hi I have a collection of the following format
{
"_id" : ObjectId("572eb5df1d739cc73c21f953"),
"address" : {
"building" : "469",
"coord" : [
-73.961704,
40.662942
],
"street" : "Flatbush Avenue",
"zipcode" : "11225"
},
"borough" : "Brooklyn",
"cuisine" : "Hamburgers",
"grades" : [
{
"date" : ISODate("2014-12-30T00:00:00Z"),
"grade" : "A",
"score" : 8
},
{
"date" : ISODate("2014-07-01T00:00:00Z"),
"grade" : "B",
"score" : 23
},
{
"date" : ISODate("2013-04-30T00:00:00Z"),
"grade" : "A",
"score" : 12
},
{
"date" : ISODate("2012-05-08T00:00:00Z"),
"grade" : "A",
"score" : 12
}
],
"name" : "Wendy'S",
"restaurant_id" : "30112340"
}
i have made 3 very simple classes to map objects. Restaurants, Address and Grade.
they have the following format
Restaurant
public String _id;
public Address address;
public String town;
public String cuisine;
public String name;
public String r_id;
public Grade [] grades;
Address
public int building;
public String street;
public long zip;
public float [] coord;
Grade
public String date;
public char grade;
public int score;
I have written the simplest of codes to just toString() a mapped object and see what i get
DB db = new MongoClient().getDB("test");
Jongo j = new Jongo(db);
MongoCollection collection = j.getCollection("restaurants");
Restaurant r1 = collection.findOne().as(Restaurant.class);
System.out.println(r1);
This code outputs
Restaurant: 572eb5df1d739cc73c21f953 / Wendy'S
Cuisine: Hamburgers
Restaurant ID: null
Address: 469 Flatbush Avenue 0, null
Grades: Tue Dec 30 05:00:00 PKT 2014 A / 8
Grades: Tue Jul 01 05:00:00 PKT 2014 B / 23
Grades: Tue Apr 30 05:00:00 PKT 2013 A / 12
Grades: Tue May 08 05:00:00 PKT 2012 A / 12
I cannot seem to fix the null I am getting for the zipcode and the restaurant_id.
Someone please help. Thanks.
Your field names don't match the documents names so they're not getting mapped over. If I recall correctly, you can use #JsonProperty to map those java field names to the document names you're getting back out of mongodb.

Categories