I need to log message in json formate. Not like this
{
"level":"INFO",
"message": "log message"
}
I want to log json object which can be
{
"level":"INFO",
"json_att1":"value1",
"json_att2": "value2"
}
There is no message field in my wish. I just want to use the pseudo code
like this log.info(jsonObj)
The essential need is I want to add custom field in the json log
You can use link library.
Example:
String str = new JSONObject()
.put("level","debug")
.put("prop1", "val1").toString();
logger.debug(str);
Related
My REST API, which is build with Spring in Java, produces an invalid JSON object, because it contains multiple breaks in a string, which lead to the problem, that the string has an unexpected end and the rest doesn't count as part of the string anymore, example:
{
"status": "Success",
"message": "Lorem ipsum",
"data": {
"correct": [
{
"record": "ULTRA LONG
XML STRING
WITH BREAKS",
"code": 0,
"errors": []
}
]
}
}
The error arises in the data -> correct -> record string field, because it contains breaks which splits the original string.
My API endpoint serializes the above JSON like this:
#PostMapping(value="/check-records",
consumes=MediaType.APPLICATION_JSON_VALUE,
produces=MediaType.APPLICATION_JSON_VALUE)
public Response checkRecords(#RequestBody(required=true) Records records) {
// Check records
return new Response("Success", "Lorem ipsum", data);
}
Response is a class, which automatically gets serialized into a JSON object after returning. data is a map in order to create the above JSON structure.
I couldn't find any suitable solution for my problem yet. Does anybody has an idea how I could remove all breaks, spaces or control characters before I serialize the JSON object?
I appreciate any kind of help, sheers! :)
Thanks to #pringi. He suggested to use a regex to remove all control characters in Java before I serialize the JSON object.
String record = orginalRecord.replaceAll("[\\\\p{Cntrl}^\\r\\n\\t]+", "")
You can find more informations about regex in the original question: How to remove control characters from java string?
I'm developing a microservice (let's call it email microservice) which generates emails from HTML templates. Basically, the client sends a json with some data to the email microservice, and based on that data it has to generate an email (populate fields in html template with values coming in the json). The client is our other microservice, which prepares json and sends to the email microservice.
Json structure would be something like this:
{
"data":{
......
},
.......
}
"Data" is the dynamic object which can contain different things. So we can populate it with pairs key - value, for example, and then map to HashMap and then use this HashMap to populate our template, using some template engine (Velocity, thymeleaf or something else). Let's say we can prepare json like this:
{
"data":{
"userName":"Jack"
},
.......
}
And then use it in the template, something like this:
<b> Hello {{userName}}! </b>
This approach works fine, but I would like to simplify it, so we wouldn't need to prepare a proper json everytime (populate "data" field with key - value pairs), instead we would like just to put there some POJOs, without carrying which fields we need, and then use JsonPath in our templates to access required data. For example we create a json:
{
"data":{
"user": {
"firstName":"Jack",
..........
},
...........
},
.......
}
And in the template write something like that:
<b> Hello {{data.user.firstName}}! </b>
That would save us some time, cause we wouldn't need to cary about json structure.
Is there any template engine which can use a Json as input and access values by JsonPath? So I write something like this: data.user.firstName and the engine automatically finds value from the given json.
The problem was solved by using Velocity and JsonPath lib.
I've created a new tool for Velocity:
public class JsonPathTool {
private final String json;
public JsonPathTool(String json) {
this.json = json;
}
public Object valueAt(String jsonPath) {
jsonPath = "$." + jsonPath;
return JsonPath.read(this.json, jsonPath);
}
}
Then I put JsonPathTool in Velocity context:
VelocityContext ctx = new VelocityContext();
JsonPathTool jsonPathTool = new JsonPathTool(json);
ctx.put("data", jsonPathTool);
And in my templates I use following syntax:
"<b> Hello $data.valueAt(\"user.firstName\") </b>"
I am working with a some multi stage forms in my Scala Play application at the moment, the end result of this multi step form is to send a POST request to an end point with this JSON structure,
{{ "name":"Company Name", "contact": { "firstname":"Firstname1", "surname":"Surname1", "email":"firstname1.surname1#xyz.com", "textPhone":false, "phone":"12222222222222" }, "address": { "addressLine1":"Address Line 1", "town":"Town1", "county":"County", "postcode":"LS1 3DE" }
}
For each form submission I am doing the following,
request.session + ("organisation_name" -> formData.toString())
Is there away that I can have this JSON structure in a session and push the data to the correct attributes? Or is there a way that I can take the session data and manipulate it into JSON that follows the above format?
One way to add something to a session is like this:
request.session.copy(
data = request.session.data + ("organisation_name" -> formData.toString())
)
Another way to add to a session at the return point is like this:
Redirect(routes.......).addingToSession("organisation_name" -> formData.toString())
Have tried Storing your JSON object in request session.
Or u can try caching the JSON object with the Timestamp then read it from cache map so when u go back to the previous from u can Repopululate by getting its attributes.
I would like to know how I can access the data from the below JSON String.
{
"posters": {
"thumbnail": "http://google.com/images/11420914_ori.jpg",
"profile": "http://google.com/images/11420914_ori.jpg"
}
}
I want this to be done in Java Lists (NOT by using JSON Parsers)
Expected output:
thumbnail: "http://google.com/images/11420914_ori.jpg"
profile: "http://google.com/images/11420914_ori.jpg"
You need a Json Mapper, I recommend you to read this : Jackson in 5 minutes
I'm starting out to collect logs with logstash. The current setup consist of a Java server using logback as logging mechanism and logstash-logback-encoder, outputting the data in a neat JSON representation. The basics work just fine.
I would like to separate additional data in JSON format in separate fields (so each key of the JSON ends up in its own field). logstash-logback-encoder provides a mechanism for that to output such data in a json_mesage field. However this JSON string is placed into a JSON array. See here a sample formatted for better reading.
{
"#timestamp":"2014-03-25T19:34:11.586+01:00",
"#version":1,
"message":"Message{\"activeSessions\":0}",
"logger_name":"metric.SessionMetrics",
"thread_name":"scheduler-2",
"level":"INFO",
"level_value":20000,
"HOSTNAME":"stage-01",
"json_message":["{\"activeSessions\":0}"],
"tags":[]
}
I tried to parse the incoming JSON using a simple JSON filter. See here my configuration:
input {
lumberjack {
<snipped>
codec => "json"
}
}
filter {
json {
source => "json_message"
}
}
output {
elasticsearch {
<snipped>
}
}
However this leads to following error in the logstash log. The JSON string in an array simply can't be handled.
{:timestamp=>"2014-03-25T19:43:13.232000+0100",
:message=>"Trouble parsing json",
:source=>"json_message",
:raw=>["{\"activeSessions\":0}"],
:exception=>#<TypeError: can't convert Array into String>,
:level=>:warn}
Is there a way to extract the JSON string from the array prior to parsing? Any help is greatly appreciated. Thanks.
Actually, it is quite simple and plays along the lines of common programming languages. Though, I did not find the answer in the docs.
Just add an index to the field in the filter:
filter {
json {
source => "json_message[0]"
}
}