I Have a Json which may come from other application and i need to check it whether is is in particular format. The JSON template i have is as follows,
{
"Types": {
"Type1": {
"attribute1": "value1",
"attribute2": "value2",
"attribute3": "value3",
"recordList": {
"record1": [
{
"field": "value"
},
{
"field": {
"subrecord1": [
{
"subfield1": "subvalue1",
"subfield2": "subvalue2"
}
]
}
}
]
},
"date": "2010-08-21 03:05:03"
}
}
}
Is there any way to validate the JSON based on particular template or format.
You can use JSON Schema for that. JSON Schema lets you describe the format of the object graph you expect to receive, and then software implementing it lets you validate what you receive against your schema. There's an OSS Java implementation called json-schema-validator.
Related
Json body:
{
"result": [
{
"object": {
"type": "mattress"
}
},
{
"object": {
"type": "pillow"
}
}
]
}
How do I assert that type is only either pillow or a mattress (there can be more so I am looking for a generic solution) using rest-assured body and hamcrest assertions?
Example assertion:
response.then().assertThat().body("result", hasSize(greaterThan(0)));
This code would solve your problem:
.body("result.object.type", everyItem(isOneOf("mattress", "pillow")));
I have a Java service that writes logs in JSON format, they are then picked up by filebeat and sent to Elastic. I would like to be able to set one of the ECS fields (event.duration) described here
I set up a net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder encoder, and I set the event.duration field in MDC before calling logging method. The output looks like this:
{
"#timestamp": "2021-12-07T10:41:59.589+01:00",
"message": "message",
"event.duration": "5606000000",
"service": {
"name": "logging.application.name_IS_UNDEFINED",
"type": "java"
},
"log": {
"logger": "com.demo.Demo",
"level": "WARN"
},
"process": {
"thread": {
"name": "main"
}
},
"error": {}
}
However, in Kibana I see event.duration as a JSON inside the flat field:
flat
{
"event.duration": "10051000000"
}
How can I make it on the same level as other ECS fields like event.name?
You should create an ingest pipeline using the dot_expander processor in order to transform your dotted field into an object:
PUT _inest/pipeline/de-dot
{
"processors" : [
{
"dot_expander": {
"field": "event.duration"
}
}
]
}
Then you need to make sure that your indexing process references this pipeline, i.e. ...?pipeline=de-dot
I have the sample request as given below. May I know how to make the request generates the key1, key2, etc. in JSON Swagger? As it seems like not a predefined object from the swagger file.
Sample request:
{
"products": {
"key1": {
"price": 99.146564,
"currencyPair": "USD-MYR",
"decimalPlace": 2
},
"key2": {
"price": 86.1571,
"currencyPair": "USD-IDR"
},
"key3": {
"price": 722.45654,
"currencyPair": "USD-ZNH"
}
}
}
Hope someone can guide me on how to solve it. Thanks.
I get below JSON string as a request body for my REST API. I don't like this JSON structure, but I don't have any control on this. It's somebody else posting this message and I have to create a REST API (POST method) and consume this message in my API. So I have to deserialize this into Java objects in my REST controller. It has list of lists objects. I tried several ways with fasterxml, but I was not successful.
{
"messages": [
[
{
"message": "message1_a",
"info": {
"timestamp": "2521013204"
}
},
{
"message": "message1_b",
"info": [
{
"message": "message1_c",
"info": {
"id": "asfa-14fs-df"
}
},
{
"message": "message1_d",
"info": {
"reason": "msg_reason",
}
}
]
}
]
]
}
Can anybody help me how my Java POJOs should look like?
It seems like a array of message.
In Java you can use Spring to transform the Json to a Object.
String url = "http://your/json/url";
ResponseEntity<Message[]> responseEntity = new RestTemplate().getForEntity(url, Message[].class);
Be sure that your entity has all the attributes of Json.
The doc: https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/client/RestTemplate.html
I am trying to parse json using jackson parser. Jackson parse the data successfully, but when #nil:true comes in the json, code breaks and gives an error:
Can not deserialize instance of java.lang.String out of START_OBJECT token
json string like this:
[
{
"Users": {
"UserID": "1",
"ExpirationDate": {
"#nil": "true"
}
}
}
]
but when json comes like below, then parsing happens successfully:
[
{
"Users": {
"UserID": "1",
"ExpirationDate": "2016-07-07"
}
}
}
]
Can you please let me know, how to handle such type of cases.