How order values in jackson codehaus java - java

I'm having an issue comparing two json because some times the json that I recive from DB is oldEncodedString has values unordered
private static boolean encodeProduct(BaseProduct baseProduct) {
boolean saveProduct = false;
try {
saveProduct = true;
String oldEncodedString = baseProduct.getEncodedJson();
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(SerializationConfig.Feature.SORT_PROPERTIES_ALPHABETICALLY, true);
objectMapper.configure(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS, false);
String json = objectMapper.writeValueAsString(baseProduct);
Objects.requireNonNull(baseProduct).setEncodedJson(json);
if (Objects.nonNull(oldEncodedString) && JSONCompare.compareJSON(oldEncodedString, baseProduct.getEncodedJson(), JSONCompareMode.STRICT).passed()) {
saveProduct = false;
}
} catch (Exception exception) {
LOG.error("Error generating encode", exception);
}
return saveProduct;
}
eg:
oldEncodedString = {
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1003", "type": "Blueberry" },
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1004", "type": "Devil's Food" }
]
}}
baseProduct.getEncodedJson =
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters":
{
"batter":
[
{ "id": "1001", "type": "Regular" },
{ "id": "1002", "type": "Chocolate" },
{ "id": "1003", "type": "Blueberry" },
{ "id": "1004", "type": "Devil's Food" }
]
}}
So the values are unordered and when I compare into that if with STRICT is said that is not the same , but because is not ordered the values I tryed adding ObjectMapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true)
but is not on this library

Related

Json Schema validation not working for Draft 7 required properties in nested object depending on a value

Request JSON:
{
"apiVersion":"0.0.9b",
"apiDate":"18.01.19",
"general":{
"documentType": "invoice",
},
"references":{
"invoiceId":"123",
"invoiceDate":"01.01.1970",
"creditNoteId":"123",
"creditNoteDate":"01.01.1970"
}
}
I have to validate request json based on documentType value if documentType is invoice then invoiceId and invoiceDate required if documentType is creditNote then creditNoteId and creditNoteDate required.
I tried the below schema but it is not working as expected
Updated JSON Schema:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": [
"apiVersion",
"apiDate"
],
"oneOf": [
{
"properties": {
"general": {
"properties": {
"documentType": {
"enum": [
"invoice"
]
}
}
},
"references": {
"required": [
"invoiceId",
"invoiceDate"
]
}
}
},
{
"properties": {
"general": {
"properties": {
"documentType": {
"enum": [
"creditNote"
]
}
}
},
"references": {
"required": [
"creditNoteId",
"creditNoteDate"
]
}
}
}
],
"properties": {
"apiVersion": {
"type": "string",
"description": "The version of the json file"
},
"apiDate": {
"type": "string",
"description": "The date when the json version was published"
},
"general": {
"$ref": "#/definitions/general_identifiers"
},
"references": {
"type": "object",
"properties": {
"invoiceId": {
"type": "string"
},
"invoiceDate": {
"type": "string"
},
"creditNoteId": {
"type": "string"
},
"creditNoteDate": {
"type": "string"
}
}
}
},
"definitions": {
"general_identifiers": {
"type": "object",
"required": [
"documentType"
],
"properties": {
"documentType": {
"enum": [
"invoice",
"creditNote"
]
}
}
}
}
}
No validation Error even if m not setting any of required field.
What have I missed?
Thanks in advance :)
The validator response is correct. Your schema requires the "references" property to be under the "general" node.
But this is not the case in your sample json (it is on the same level, not as child)
"general":{
"documentType": "invoice",
},
"references":{
...}

Apache Velocity: remove key/value from json

I have a JSON
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"card": {
"expiryMonth": "1",
"expiryYear": "2033"
},
"order": {
"amount": 1
}
}
}
}
And I want to remove the card info of it like this:
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"order": {
"amount": 1
}
}
}
}
How can I do this with Apache velocity?
What works is:
#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.replaceAll("2033","2055"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}
This gives me
{
"Id": "xxx",
"Type": "Transaction.Create",
"Payload": {
"result": 2,
"description": "Pending",
"body": {
"redirect": {
"url": "xxx",
"fields": {
"MD": "8a829449620619e80162252adeb66a39"
}
},
"card": {
"expiryMonth": "1",
"expiryYear": "2050"
},
"order": {
"amount": 1
}
}
}
}
But now I want to remove the card part but it does not work:
#set($content = $util.urlEncode($input.json('$')))
#set($new = $content.delete("$.Payload.body.card"))
Action=SendMessage&MessageBody={"body": "$new","Event-Signature": "$util.urlEncode($input.params('Event-Signature'))"}
what am I doing wrong?
Main goal is transform a mapping template in API Gateway for a webhook. The webhook contains to many information and we want to remove some part of the JSON POST call.
Try using the below
#set($dummy=$content.Payload.remove("card"))

Elastic Search, creating index with sources and settings by Rest Java Client

I'm trying to create index following this guide: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-create-index.html#_providing_the_whole_source
The problem is, that index is not created properly. Looks like whole settings section as well as completion type is ignored.
My json file:
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1,
"analysis": {
"filter": {},
"analyzer": {
"keyword_analyzer": {
"filter": [
"lowercase",
"asciifolding",
"trim"
],
"char_filter": [],
"type": "custom",
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"first": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
},
"second": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
},
"third": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
},
"fourth": {
"type": "text",
"fields": {
"keywordstring": {
"type": "text",
"analyzer": "keyword_analyzer"
},
"completion": {
"type": "completion"
}
},
"analyzer": "standard"
}
}
}
}
}
Java code:
CreateIndexRequest indexRequest = new CreateIndexRequest(ESClientConfiguration.INDEX_NAME);
URL url = Resources.getResource(TERYT_INDEX_CONFIGURATION_FILE_NAME);
return Try.of(() -> Resources.toString(url, Charsets.UTF_8))
.map(jsonIndexConfiguration -> indexRequest.source(jsonIndexConfiguration, XContentType.JSON))
.get();
createIndexRequest.setTimeout(TimeValue.timeValueMinutes(2));
Try.of(() -> client.indices().create(createIndexRequest, RequestOptions.DEFAULT))...
Index is created but when I look into Index Metadata, it looks completly wrong:
{
"state": "open",
"settings": {
"index": {
"creation_date": "1556379012380",
"number_of_shards": "1",
"number_of_replicas": "1",
"uuid": "L5fmkrjeQ6eKmuDyZ3MP3g",
"version": {
"created": "7000099"
},
"provided_name": "my_index"
}
},
"mappings": {
"my_type": {
"properties": {
"first": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"second": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"third": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
},
"fourth": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
},
"aliases": [],
"primary_terms": {
"0": 1
},
"in_sync_allocations": {
"0": [
"Cx6tBeohR8mzbTO74dwsCw",
"FcTUhpb_SL2LiaEyy_uwkg"
]
}
}
There is only 1 shard without replicas, I also don't see any informations about completion type. Does someone could tell me what I'm doing wrong here?
I think that this line:
Try.of(() -> client.indices().create(createIndexRequest, RequestOptions.DEFAULT))...
is hiding an important exception.
Here you are using elasticsearch 7.0.0 which does not allow anymore giving a "type" name in your mapping.
Instead of
"mappings": {
"my_type": {
"properties": {
You should write:
"mappings": {
"properties": {
Because of the exception and the fact that probably after index creation you are indexing some documents, default index settings and mapping are applied.
Which explains what you are seeing.
You need to fix your index settings first.
I'd recommend doing that in Kibana dev console.

Insert object to nested array

I'm trying to insert an object to a nested array through the java api, but I get the following error
MapperParsingException[object mapping for [X] tried to parse field [null] as object, but found a concrete value]
doing it through Kibana as shown below the same script works.
Any ideas on how to fix this?
The java code is the following
HashMap<String, Object> params = new HashMap<>();
params.put("object", objectAsString);
Script script = new Script(ScriptType.INLINE, "painless", "ctx._source.media.add(params.object)", params);
UpdateResponse result = elasticClient.prepareUpdate(indexName, "Type", documentId).setScript(script).execute().actionGet();
Trhough Kibana
POST index/document/id/_update
{
"script": {
"lang": "painless",
"inline": "ctx._source.media.add(params.object)",
"params": {
"object": {
"description" : "A second image",
"height" : 5,
"weight": 5,
"name" : "Test",
"orientation" : "Vertical",
"url" : "htttp://newurl.jpg",
"tags":["first","second"],
"type":"image"
}
}
}
}
The template mapping is the following:
"mappings": {
"FOLDER_MODULE": {
"properties": {
"name": {
"type": "keyword"
},
"publisherId": {
"type": "keyword"
},
"username": {
"type": "keyword"
},
"media": {
"type": "nested",
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"publisherId": {
"type": "short"
},
"tags": {
"type": "text"
},
"description": {
"type": "text"
},
"url": {
"type": "keyword"
},
"createdDate": {
"format": "strict_date_optional_time||epoch_millis",
"type": "date"
},
"height": {
"type": "float"
},
"width": {
"type": "float"
},
"weight": {
"type": "float"
},
"orientation": {
"type": "keyword"
},
"status": {
"type": "keyword"
},
"type": {
"type": "keyword"
},
"username": {
"type": "keyword"
}
}
}
}
}
}

Parsing JSON using GSON with multiple inner objects.

I would like to parse the following JSON response, using GSON. As you can see, there are a lot of inner json objects in the response. Do I need to create POJO classes for all the inner objects or is there any alternative?
{
"nodeProperties": [
{
"properties": {
"timeStamp": {
"value": 1400475483694,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:03"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "NEXT_NEWSwitch3"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:03",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1400475481261,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:02"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:02",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1400475478695,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:01"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
}
}
]
}
Simply convert the JSON string into Map.
Sample code:
FileReader in = new FileReader(new File("resources/json3.txt"));
BufferedReader br = new BufferedReader(in);
// Convert JSON string into MAP object
Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<Map<String, Map<String, Object>>>>>() {}.getType();
Map<String, ArrayList<Map<String, Map<String, Object>>>> map = gson.fromJson(br, type);
for (String key : map.keySet()) {
System.out.println(key);
for (Map<String, Map<String, Object>> value : map.get(key)) {
for (String k : value.keySet()) {
System.out.println(k);
for (String k1 : value.get(k).keySet()) {
System.out.println(k1 + ":" + value.get(k).get(k1));
}
}
System.out.println("--------------");
}
}
in.close();
br.close();
You can do it using some POJO classes as well which are replica of the JSON string that is more simple and shorter.
Sample code:
class PropertiesNode {
Properties properties;
Node node;
// getter & setter
}
class Node {
String id;
String type;
// getter & setter
}
class Properties {
Map<String, Object> timeStamp;
Map<String, String> macAddress;
Map<String, Integer> tables;
Map<String, Integer> capabilities;
Map<String, Integer> tier;
Map<String, String> supportedFlowActions;
Map<String, Integer> buffers;
Map<String, String> description;
Map<String, Integer> forwarding;
// getter & setter
}
Gson gson = new Gson();
Type type = new TypeToken<Map<String, ArrayList<PropertiesNode>>>() {}.getType();
Map<String, ArrayList<PropertiesNode>> nodeProperties = gson.fromJson(br, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(nodeProperties).toString());
output:
{
"nodeProperties": [
{
"properties": {
"timeStamp": {
"value": 1.400475483694E12,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:03"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "NEXT_NEWSwitch3"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:03",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1.400475481261E12,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:02"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"tier": {
"value": 1
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:02",
"type": "OF"
}
},
{
"properties": {
"timeStamp": {
"value": 1.400475478695E12,
"name": "connectedSince"
},
"macAddress": {
"value": "00:00:00:00:00:01"
},
"tables": {
"value": -1
},
"capabilities": {
"value": 199
},
"supportedFlowActions": {
"value": "[Controller, Drop, Enqueue, HwPath, Output, PopVlan, SetDlDst, SetDlSrc, SetNwDst, SetNwSrc, SetNwTos, SetTpDst, SetTpSrc, SetVlanId, SetVlanPcp, SwPath]"
},
"buffers": {
"value": 256
},
"description": {
"value": "None"
},
"forwarding": {
"value": 0
}
},
"node": {
"id": "00:00:00:00:00:00:00:01",
"type": "OF"
}
}
]
}

Categories