How to validate json schema for a particular naming? - java

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!.

Related

how to get a equal-match result from field which has array value in elasticsearch

In my documents, I have a field which has an array of String.
like
weapon : ["Bland Blade", "Defender Quelthalas", "Thousand Lies", "Frozen Bonespike"]
I would like to get all the documents whose weapon field has "Frozen Bonespike".
not field which has one word - like "Frozen" or "Bonespike"
and not even field which contains "Frozen Bonespike" - like "Winter Frozen Bonespike"
I would like to get a field which has exactly equal string with query word
Have you any idea?
To achieve that you have to set your weapon field as type keyword and then run a terms search against it.
By default elasticsearch will create both text type field (lets say weapon) and keyword (weapon.keyword)
Ingesting document
POST test_seo/_doc
{
"weapon": [
"Bland Blade",
"Defender Quelthalas",
"Thousand Lies",
"Frozen Bonespike"
]
}
Query
POST test_seo/_search
{
"query": {
"term": {
"weapon.keyword": {
"value": "Frozen Bonespike"
}
}
}
}
If you want to search by many different weapons you can use a terms query
POST test_seo/_search
{
"query": {
"terms": {
"weapon.keyword": [
"Bland Blade",
"Thousand Lies"
]
}
}
}

How to update nested JSON in Java with dynamic structure

I have a property file, where the user can define full field names, something like:
league.firstname
league.lastName
Now in my code, I have an incoming JSON, and I want to first retrieve the existing value and then edit the values of fields mentioned in the property file. In this case, retrieve and edit values of league.firstName and league.lastName fields. JSON looks something like:
{
"_internal": {
"pubDateTime": "2017-12-10 11:42:23.504",
"xslt": "xsl/league/roster/marty_active_players.xsl",
"eventName": "league_roster"
},
"league": {
{
"firstName": "Alex",
"lastName": "Abrines",
"personId": "203518",
"teamId": "1610612760"
}
}
}
Now I know I can do something like:
// retrieve values
String fName = jsonObj.getJSONObject("league").getString("firstName");
String lName = jsonObj.getJSONObject("league").getString("lastName");
// put new values
jsonObj.getJSONObject("league").put("firstName", "newVal1");
jsonObj.getJSONObject("league").put("lastName", "newVal2");
But how do I achieve this dynamically. For example, property file is like:
parentObj.child1.child2.child3
and now I want to edit this 4 level deep JSON, so want to do something like:
// how to do the following
String oldVal = jsonObj.getString("parentObj.child1.child2.child3");
jsonObj.put("parentObj.child1.child2.child3", "newVal");
Note:
the level of json depth can be 1 < length < 100.
user can put any relevant value in property file. If the key do not exist in JSON, need to catch exception.
incoming json format is NOT fixed.

How to compare 2 json by ignoring some attributes and child element orders

I'm seeking for a java library which can give fine grain control on json comparision. (just like XMLUnit)
For example, I have below 2 json document:
Control:
{
"timestamp":1234567,
"items":[
{
"id":111,
"title":"Test Item 111"
},
{
"id":222,
"title":"Test Item 222"
}
]
}
Test:
{
"timestamp":7654321,
"items":[
{
"id":222,
"title":"Test Item 222"
},
{
"id":111,
"title":"Test Item 111"
},
{
"id":333,
"title":"Test Item 333"
}
]
}
I'd like to apply below semantics when comparing them:
Ignore 'timestamp'
When comparing 'items', please do head to head compare against 'id'
Any suggestions?
As point out in your tags you can use Jackson to convert the both json to a Java class.
You can override the equals method within the class with your desired condition.
After all just use equals function in your objects.
JsonUnit seems to help:
https://github.com/lukas-krecan/JsonUnit#options
for my case:
Configuration cfg = Configuration.empty().when(path("items"), then(Option.IGNORING_ARRAY_ORDER, Option.IGNORING_EXTRA_ARRAY_ITEMS)).when(path("timestamp"), thenIgnore());
Diff diff = Diff.create(control, test, "", "", cfg);
assertTrue(diff.similar());

How to check if a property exists in a JSONObject without calling .getJSONObject(key) several times?

I'm processing deeply nested JSON data. Here is a shortened example:
{
"timestamp":"123",
"layers":{
"frame_raw":"123",
"frame":{
"frame_frame_interface_id":"0",
"frame_interface_id_frame_interface_name":"asd",
...
},
"eth_raw":"123",
"eth":{
"eth_eth_dst_raw":"asd",
"eth_eth_dst":"asd",
...
},
"ip_raw":"123",
"ip":{
"ip_ip_version_raw":"4",
"ip_ip_version":"4",
"ip_ip_addr_raw":[
"asd",
"asd"
],
"ip_ip_addr":[
"1.1.1.1",
"1.1.1.1"
],
"ip_ip_dst_host":"1.1.1.1"
}
...
}
...
}
I have a list of structures that I explicitly allow. All others should be deleted from the JSON. An example for the list:
###frame###
layers.frame_raw
###eth###
layers.eth.eth_eth_dst
layers.eth.eth_eth_src
###ip###
layers.ip.ip_ip_src
layers.ip.ip_ip_dst
layers.ip.ip_ip_src_host
layers.ip.ip_ip_dst_host
layers.ip.ip_ip_version
layers.ip.ip_ip_hdr_len
layers.ip.ip_ip_dsfield
My problem is that I can only navigate through the JSON structure using "getJSONObject(key)".
How can I generate the path of the lowest elements of my JSON structure in the form "key.key.key..." so that I can match it with my list?
JSON object has a method called .has which will return a boolean value. So the logic is like to check whether the JSON object exists. If so check the child node for more existence of your data. If there is an array you have to get the array and migrate through the array using any loop and do the same. For example:
if (json.has("ip")) {
JSONObject jsonObject = getJSONObject.getString("ip")
String ip_ip_version_raw = jsonObject.getString("ip_ip_version_raw"));
if (jsonObject.has("ip_ip_addr_raw")){
JSONArray ip_ip_addr_raw = jsonObject.getJSONArray("ip_ip_addr_raw");
for (String s : ip_ip_addr_raw) {
...
}
}
}
For reference :
http://developer.android.com/reference/org/json/JSONObject.html#has(java.lang.String)
If you want to check the existence of a property of a JSONObject there is a method called has. This is the link to the official documentation.
I found a solution because the structure in my special case is always layers.layer.property, so the depth of the structure is always the same and not variable.
For interested people:
for (Iterator<String> layersIterator = jsonObject.getJSONObject("layers").keys(); layersIterator.hasNext(); ) {
String layer = layersIterator.next();
for (Iterator<String> propertyIterator = jsonObject.getJSONObject("layers").getJSONObject(layer).keys(); propertyIterator.hasNext(); ) {
String property = propertyIterator.next();
System.out.println("layers."+layer+"."+property);
}
}

How to get nested documents by field value from other documents (ElasticSearch)?

example query:
localhost:8080/content/child/123
This query should return doc where parent id is equal to "123", then I need to get id from the returned document and search in Elastic for documents where parent id is equal to this id.
example result :
{
"id": "test",
"parentId" : "123",
"name" : "bambo"
}
{
"id": "someId",
"parentId" : "test",
"name" : "bambo 2"
}
First of all, GET /content/child/123 means searching for the specific document where _index=content; _type=child; _id=123, which is not what you're looking for. The _id field is completely different from your id and parentId fields.
As far as I know, ES does not currently have a "nested search" feature as you described. You need to do two separate searches.
To search for documents with field ("parentId") containing a specific value ("123"), you need the following search query
{
"query": {
"bool": {
"filter": {
"term": {
"parentId": "123"
}
}
}
}
}
After your first search, the response will be a JSON object resp. You may find a list of returned results in resp["hits"]["hits"]. Then, parse a result object to obtain the id field you want. For example, resp["hits"]["hits"][0]["_source"]["id"] will give you the id field.
Check out the documentation here https://www.elastic.co/guide/en/elasticsearch/reference/current/_the_search_api.html

Categories

Resources