Convert escaped string to json object in java - java

I have a string that looks like this..
"Message" : "{\"eventType\":\"DataChanged\",\"operation\":\"create\"}"
Is there any library in Java that can convert this to the string below so I can then parse it into a json object
"Message" : {"eventType":"DataChanged","operation":"create"}

Related

How to convert String to Json Object in java or groovy

I saved a json into the database as a string like this:
"[_district:_1_2_5village, _name:_1_1_2id_inter, _gender:_1_3_5sex]"
Now i want to convert it back to a Json Object so as to pick the key and value eg _district is the key and _1_2_5village is the value. Any help on how i can achieve this. Thanks
I tried to convert the string back into JSON by parsing but that dint work for me.
It doesn't work because that's not a JSON format, a JSON is a way of mapping objects and uses key value syntax like so:
{"key": "value"}
and an array would look like this:
[{"key": "value"},{"key": "value"}]
You'll need to make a custom parser for your syntax
Here's the json specification:
https://www.json.org/json-en.html

JSON formatting in Apex

I am getting a JSON in below format:
{
"A":"1",
"B":"2"
}
I have a field update and JSON at some point could be too long.
How can I change the JSON format to below pattern?
{"A":"1","B":"2"}
I am trying to store this minified JSON format on a field, so that char limit issue is resolved.
deserialize the string and serialize it again and then update the field.
Declare the JSON as string
Map<String,Object> parser = (Map<String,Object>)JSON.deserializeUntyped(yourstring);
yourstring = JSON.serialize(parser);
You can change Map accordingly. Required format will be present in string variable.

Read not correct JSON

I have JSON as string
"{nameBitsCount=131}"
I need, using Jackson: 1) Parse this JSON correctly. 2) Put result into Map<String, Long>
But I Getting exception.
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('n' (code 110)): was expecting double-quote to start field name
at [Source: (String)"{nameBitsCount=131}";
PS: I think I need to change style of JSON to this
"{\"nameBitsCount\":\"131\"}";
The JSON you send isn't valid
{nameBitsCount=131}
need to convert format :
{"nameBitsCount":131}
add {"} around the Key and change {=} to {:} the JSON format valid for Json Object :
{
"Key" : "Value"
// "VALUE" if use for String and char put {"} around the value VALUE ex: 0.0 , 1 , -50 , FALSE
}
Look this website :
JSON Website
JSON Syntax

Java escapes quotes in JSON strings

I've got a JSON string persisted in a DB (in a column of type Text), which I would like for clients of the server to be able to retrieve through a REST API.
Problem is that once the JSON string is retrieved by the backend and stored in a String object, ready to be returned to the client as part of the response body, Java automatically escapes the quotes.
So a JSON string persisted as:
{"key1": "value1", "key2": "value2"}
Would be retrieved and stored in a string object as:
{\"key1\": \"value1\", \"key2\": \"value2\"}
Then this escaped string would be returned to the client. My worry is that in the format that it is it won't be usable by the client.
I've seen some people using regex to remove the backslash, but I'm not sure how feasible that would be as a solution.
Any suggestions?
You can use org.apache.commons.text.StringEscapeUtils class for doing this. Please refer below code
String s = "{\\\"key1\\\": \\\"value1\\\", \\\"key2\\\": \\\"value2\\\"}";
s = StringEscapeUtils.unescapeJava(s);
System.out.println(s);
gives
{"key1": "value1", "key2": "value2"}

Convert XML with . attribute to JSON in java

I have an XML with the attribute <name.first>.
I want to convert this XML to JSON and save it to MongoDB.
I am using org.json.XML to convert it to JSONObject with:
XML.toJSONObject(xml)
The converted JSON contains the key: { ... , name.first : ... }, which is not supported with MongoDB.
Is there a way to serialize the XML converter so that the conversion would output something else like name_first ?

Categories