Lambda Java - Iterate a Json Object. It looks like inside is duplicated - java

I have a json like this and I use forEach to go over family
{
"people": {
"family": [
{
"id": "123",
"member": [
{
"id": "11",
"salary": false
},
{
"id": "12",
"salary": false
}
]
},
{
"id": "124",
"member": [
{
"id": "11",
"salary": false
},
{
"id": "12",
"salary": false
}
]
}
]
}
}
For instance, if I set the salary in true in the first member id 123, when i go through the second element of family (id 124), salary is also in true.
If I change something in the second one id 124, the first id 123 has the same value. It looks like every family element it's the same object but not sure how I can find out that and also how to fix it.
The code is really big, but basically I'm doing something like this:
.forEach(family -> {
family.getId() // I get 123 and then 124
family.getMember().forEach(member -> {
System.out.println(member.getSalary()) // Show false for id 123 but then for id 124, show true when i would be false at this point
member.setSalary(true);
Any idea? not sure what else I can do.... Thanks

Check if objects in member collection for 123 and 124 are different, have different reference. It sounds like those objects has the same reference in those two collections. Try maybe to override equals and hashCode.

so I am assuming the corrected result should be:
false
false
false
false
this statement member.setSalary(true); is shouldn't be affected
"member" is should be in separate object it shouldn't refer to same reference (pretty weird)
can u post the full block of code?

Related

Is there a way to extract all values by some key from a list of maps using SpEL?

Let's assume there's a map corresponding to the following structure:
{
"lists": [
{
"list": [
{
"letter": "a"
},
{
"letter": "b"
}
]
},
{
"list": [
{
"letter": "c"
},
{
"letter": "d"
}
]
}
]
}
There's an easy way to get all lists using SpEL (#root['lists']) or all letters of the first list ("#root['lists'][0]['list']") or the first letter of the first list ("#root['lists'][0]['list'][0]"). Also, there's a projection mechanism which allows using construction like "#root['lists'].![#this['list']]" to convert each item of lists to a result of the projection expression.
However, given all these possibilities, I still failed to come up with an expression allowing me to extract all letters from both lists. For the example above, I'd like to get
[
{
"letter": "a"
},
{
"letter": "b"
},
{
"letter": "c"
},
{
"letter": "d"
}
]
I tried to use the projection mechanism to achieve my goal but it didn't really help. The problem I see is that every time SpEL detects a list, it applies the projection expression to each element of the list so any structure of nested lists can't be changed this way.
The problem I solve can be easily solved using JsonPath, however, I assume I could get something wrong and didn't notice a way to achieve the same result using SpEL. I would be happy to listen to any ideas.

ShEx Validation - reason and appInfo are null in Result Shape Map

I am learning ShEx and using 'shexjava API' done by http://shexjava.lille.inria.fr/ for my project. I have schema, data graph and fixed shape map. When I validate using refine and recursive validation, I am getting ResultShapeMap but the reason and appInfo are null for NONCONFORMANT status. I do not understand why these two fields are null.
I have schema, dataGraph, shapeMap. This is code for validation.
ValidationAlgorithm vl = new RefineValidation(schema, dataGraph);
ResultShapeMap result = vl.validate(shapeMap);
Shape is,
{
"#context": "http://www.w3.org/ns/shex.jsonld",
"type": "Schema",
"shapes": [
{
"id": "http://example.com/ns#HouseShape",
"type": "Shape",
"expression": {
"type": "EachOf",
"expressions": [
{ "type": "TripleConstraint",
"predicate": "http://example.com/ns#number",
"valueExpr": { "type": "NodeConstraint",
"datatype": "http://www.w3.org/2001/XMLSchema#String"
}
},
{ "type": "TripleConstraint",
"predicate": "http://example.com/ns#size",
"valueExpr": { "type": "NodeConstraint",
"datatype": "http://www.w3.org/2001/XMLSchema#decimal"
}
}
]
}
}
]
}
Data is,
ex:House1 a ex:House ;
ex:number "11A" ;
ex:size 23 .
My Result is,
ResultShapeMap [
associations= [
ShapeAssociation [
nodeSelector=<example.com/ns#House>,
shapeSelector=<example.com/ns#HouseShape>,
status=NONCONFORMANT,
reason=null,
appInfo=null
]
]
]
I want to output the reason for not conforming. But it gives me null for that.
Could some one please help me.
The shexjava implementation currently does not support indicating a reason for failure.
This is because when a node does not satisfy a shape there may be several reasons.
If you want to learn ShEx, I would advise you to use ShapeDesigner
https://gitlab.inria.fr/jdusart/shexjapp/
which provides a graphical interface in which you can explore validation results.
In this particular case, it indicates that the validation fails because 23 is not a decimal (it's actually an integer) Screenshot of validation exploration result in ShapeDesigner
I do not know whether this is a bug, i.e. whether integrers should be considered to be also decimals in RDF.

How to validate json schema for a particular naming?

Updated:
Please, take a look at following.
For this example, it has two pretty the same objects inside. With two fields for each of them - id and is_deleted.
{
"meta": {
"delete_count": 1,
"status": [
{
"id": 1,
"is_deleted": true
},
{
"id": 2,
"is_deleted": false
}
]
}
}
Let's imagine 2 different occasions:
every id has changed to uid
status has changed to stat, the only is_deleted field has changed to just deleted(if this is possible. let's imagine it is)
So, I have a schema for this payload. But, it checks for field types not for field names.
it uses com.jayway.restassured.module.jsv
JsonObject has a method which returns true if key exist public boolean has(java.lang.String key)
JSONObject jsonObj = new JSONObject(Your_STRING);
if (jsonObj.has("org_id")) {
//Do stuff
}
for more details you can check below -
check key exist
try to verify if org_id exists, the question isn't clear!.

Java - Dynamic JSON creation with selected option

I need to prepare JSON object like below, based on the user selected option dynamically. What is best approach to dynamically set value of "selected" key in JAVA. I have if-else and Switch in my mind but I am looking for better approach.
[{
"name": "By Distance",
"selected": "FALSE"
},
{
"name": "By Rating",
"selected": "TRUE"
},
{
"name": "By Cost",
"selected": "FALSE"
}
]

increment nested JSONDBObject value in java

I have a BasicDBObject of following structure in JAVA
"category_id": "keyboard",
"ontology_id": "mobile",
"path": "#mobile#keyboard",
"date": "2013/008/01 12:30:00",
"hour": {
"12": {
"total_docs": 1
}
}
I need to increment total_docs value by 1 but i am not able to access that object. How should i proceed in this case
never mind, we just need to append the value in string and it's done

Categories

Resources