I am using ObjectUtils.isEmpty() as generic to check for the null and empty fields.It was working fine for the term like:
MyClass class = new MyClass()
class.setName(null);
if (!ObjectUtils.isEmpty(class.getName)) {
return "Name";
}
But it is not working , when am trying it for the Object as below:
Object activatedDate = jsonObject.getJSONArray("records")
.getJSONObject(i).get("ActivatedDate");
return ObjectUtils.isEmpty(activatedDate) ? null : "hello";
and My json is:
[
{
"LastModifiedDate": "2018-05-23T10:58:18.000+0000",
"Order_Submission_Outcome__c": "test",
"Number_of_Orders__c": 0,
"Sales_Code__c": null,
"Opportunity__c": "FERERR",
"Contract_Type__c": "EZ Order Form",
"StatusCode": "Draft",
"CreatedDate": "2018-05-23T10:56:22.000+0000",
"Id": "FERERR",
"ActivatedDate": null,
"Contract_Status__c": "ACTIVE",
"Opportunity_Act_Total_Lines__c": 1
}
]
But it always returns hello even though the object activatedDate is null. What is the problem?
In JPA QL if you want to call a database function it has to be done like this:
function(‘FUNC_NAME’, param1, param2,...)
Related
After my RestAssured code executed, I got a response body like this(which is valid, since I verified it in Postman also)
[
{
"team": null,
"name": "Automation",
"id": "977638c2-0095-42fb-a3fb-3ef442cc61e2"
},
{
"team": null,
"name": "Default",
"id": "e9e0ab5d-0abf-402a-a747-612a4e9c7e25"
}
]
Now I need to retrieve the id of a Json object for which the name is 'Automation'. The line of code I wrote is like this which is returning null, which I can't understand.
response.path("$.find{it.name == 'Automation'}.id"); (OR)
response.path("content.find{it.name == 'Automation'}.id");
Please correct my code, to return the correct value 977638c2-0095-42fb-a3fb-3ef442cc61e2
You can use the query like this:
response.path("find{it.name == 'Automation'}.id");
I'm passing a json to objectmapper. The JSON string looks like this:
{
"id": "ID1",
"identifier" : "XYZ",
"data": [
{
"id": "sampleParentID",
"childrenElements": [
{
"id" : "sampleChildID",
"content" : "sample child content",
}
]
}
]
}
val objectMapper = ObjectMapper().registerModule(KotlinModule())
val obj: Object1 = objectMapper.readValue(jsonString)
My class looks something like this :
data class Object 1 (
var id : String? = null,
var identifier : String? = null,
var data: MutableList<Element>? = null,
){
// some functions
}
class Element (
var id : String?= null
var content : String? = null
var children: List<Element>? = listOf(),
) {
// som functions
}
From obj, data field is nested which is an object itself.
I want to get hashCode of data so I do obj.data.hashCode(). Let's say 12345 gets generated.
I store this in the database. Now, let's say the user sends another request with exactly the same JSON, again the JSON gets converted into an object from which I extract the data field and now when I do obj.data.hashCode(), 12345 is not generated, rather some other number gets generated.
Is this behavior expected? If yes, what is the workaround?
Update : Added classes description.
Given that your Element class is not a data class (in this case you would get a hashCode() method implementation based on all class properties) you will need to write the hashCode() method yourself so that the default one (based on object memory reference) is not used and you get rid of the behaviour you are currently facing.
Edit Found Solution:
Found the error in my code. I'm running this on a unit test and Android doesn't use JSON objects on unit tests because it's part of android. That's why it was returning null.
Question:
I'm tying to convert my String back to a JsonObject using JSONObject("string")
Here is a sample of my String:
{
"sessions": [
{
"locations": [
{
"lat": "14.2294625",
"lng": "121.1509005",
"time": 1560262643000,
"speed": 0,
"speedLimit": 0
},
{
"lat": "14.2294576",
"lng": "121.1509498",
"time": 1560262713000,
"speed": 0,
"speedLimit": 0
},
{
"lat": "14.2294576",
"lng": "121.1509498",
"time": 1560262714000,
"speed": 0,
"speedLimit": 0
}
],
"name": "1.5645220491E12"
}
]
}
Its returning null on my JsonObjects:
content = "the string above"
var obj = JSONObject(content.substring(content.indexOf("{"), content.lastIndexOf("}") + 1))
System.out.println("obj1: " + obj.toString())
obj = JSONObject(content)
System.out.println("obj1: " + obj.toString())
var obj1 = JSONArray(content)
System.out.println("obj1: " + obj1.toString())
obj1 = JSONArray(content.substring(content.indexOf("{"), content.lastIndexOf("}") + 1))
System.out.println("obj2: " + obj1.toString())
All the outputs of this are null. Any way to know what error happens so I can adjust my json string?
Since you are running your code with Junit, it running on the SDK on your computer. This SDK doesn't provide everything, some are just some skeleton class providing signature of method and documentation but not the code. So you can't execute directly.
Try to add the library in testing
testImplementation 'org.json:json:your_version'
See the version here :
http://mvnrepository.com/artifact/org.json/json
This is based on the answer on this post: JSONObject returns a non null value of "null" after instantiating with string
There is no need to substring your JSON string. Put it inside JSONObject and it will work fine.
If you need to get neasted objects use getJSONObject or getJSONArray methods
Show your content string in code (or how do you load it)
Your edited code
val content = "the string above"
var obj = JSONObject(content)
I run this funtion to parse your string and it's ok. Your string is valid. Can you specify how do you init val content
fun parseJson() {
var stringJson = "{\"sessions\": [{\"locations\": [{\"lat\": \"14.2294625\",\"lng\": \"121.1509005\",\"time\": 1560262643000,\"speed\": 0,\"speedLimit\": 0},{\"lat\": \"14.2294576\",\"lng\": \"121.1509498\",\"time\": 1560262713000,\"speed\": 0,\"speedLimit\": 0},{\"lat\": \"14.2294576\",\"lng\": \"121.1509498\",\"time\": 1560262714000,\"speed\": 0,\"speedLimit\": 0}],\"name\": \"1.5645220491E12\"} ]}"
var obj = JSONObject(stringJson)
System.out.println("obj1: $obj")
var sessionArray: JSONArray = obj.optJSONArray("sessions")
System.out.println("obj1: $sessionArray")
var firstObject = sessionArray[0]
System.out.println("obj1: $firstObject")
}
I have following mongodb document structure
{
"_id": ObjectId("571530da41995a703faaf55b"),
"project_name": "abc",
"status": "new",
"cluster": "",
"sku": {
"_id": "57152f7941995a703faaf559",
"name": "temp",
"distribution": "apache",
"creation_date": "2016-04-18T19:02:46.595Z",
"user_id": "570784b1682d25f45b64ed51",
"__v": 0,
"services": []
}
}
I want to find project info given a sku._id. I am using following piece of code for this purpose.
String getProjectIDforSku(String skuID ) {
ObjectId id = new ObjectId(skuID);
BasicDBObject query = new BasicDBObject("sku", new BasicDBObject("_id", skuID));
DBObject obj = getCollection("deployment", "projects").findOne(query);
if (obj != null) {
var jsonString = Json.parse(obj.toString());
return jsonString;
} else
return null;
}
When I call this function, I receive null instead of valid json string.
Can anyone guide me where i am wrong?
Thanks
Change
BasicDBObject query = new BasicDBObject("sku", new BasicDBObject("_id", skuID));
to
BasicDBObject query = new BasicDBObject("sku._id",skuID);
The reason being that in the first one you are comparing sku to an object with only one field _id (which is not the case, the object has other fields), while the second one only matches the _id field within the sku object.
i have a JSON like example below and i'm trying to get some values, for example value of.
results.shipper.id
{
"results": [
{
"updated": false,
"notification": false,
"some_data": {
"id": 15989,
"pieces": 0,
},
"shipper": {
"updated": false,
"notification": false,
"id": 1587,
"parent": {
"updated": false
},
I'm trying to get value by this way:
String test = shipmentData.getJSONObject("shipper").getString("id");
But it always throws a exception. I think, that exception is caused because of the i am not accessing to the values via "results" array.
How can i easy access to the value what i need.
I tried find some helpers (Gson, fast-json, etc..) but it seems to be a quite complicated for using (i would like to work with JSON tree for direct access to values "on-the-fly" or access to values like to a object, it means.. Object.InnerObject.value ).
So question is how can i do it right?
Thanks for any advice.
JSON needs to be traversed in order to access id:
JSONArray results = shipmentData.getJSONArray("results");
JSONObject first = results.getJSONObject(0);
JSONObject shipper = first.getJSONObject("shipper");
Integer id = shipper.getInt("id");
Parse int to string:
String id = String.valueOf(shipper.getInt("id"));