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.
Related
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
I’m using GSON to convert a JSON string stored in our DB to our corresponding object class.
String configData = dbCall.fetchPublishedData(dbURL); //returns JSON string
Gson gson = new Gson();
publishDataObject = gson.fromJson(configData, PublishedDataVO.class);
For the most part, this is working, except for one particular row in our DB that is failing with an exception from the GSON parser. All of our rows in the DB are created with the same tool and have the same data structure, the only difference for this one row is that it holds A LOT of data in its JSON string. When I try to retrieve this data and transform it into our class object, I am getting the following error:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Unterminated string at line 1 column 4001 path...
The size of the data string in this row is the only difference with the other row's strings. I even took the JSON string and formatted it to see if it is a valid JSON string, and it doesn't show errors. Does GSON have a limit on the input string size for its fromJSON method?
Am using http://jsoniter.com/ java lib to parse json , it seems with this lib , it only possible to iterate over through the JSON and did not provide any api to get value for specific key like we have in org.json like below
jsonObject.get("some_key")
so do we have such type of getter methods in http://jsoniter.com/ also , can any one please help me in that .
This is probably already too late, but you can do it like this:
String jsonString = "{'a':1,'b':'text'}".replaceAll("'", "\"");
Any jsonObject = JsonIterator.deserialize(jsonString);
long number = jsonObject.get("a").toLong();
String text = jsonObject.get("b").toString();
Is there any out of box method in java (groovy) that converts the string like
String s = "[a:12,b:[a:b,c:d]]";
to a Map object with key value pairs.
Update: It is somehow similar to the question asked as Groovy: isn't there a stringToMap out of the box?(Groovy: isn't there a stringToMap out of the box?). The difference is here the keys are of string type which makes easier to parse, since i have retrieved the mentioned map by doing .toString() i am unable to parse by the answered methods because my string actually contains date strings which i need it back to date objects. So it was difficult to parse the whole string.
You can convert this string to JSON-like format, and simple parse it:
String s = "[a:12,b:[a:b,c:d]]";
def result = new JsonSlurper().setType(JsonParserType.LAX).parseText(s.replaceAll('\\[', '{').replaceAll('\\]', '}'))
I have a way that may work depending on your actual data. The problem with your sample data is that you supply unquoted strings b and d as part of the structure:
String s = "[a:12,b:[a:b,c:d]]";
If what you had was actually
String s = "[a:12,b:[a:'b',c:'d']]";
or
String s = "[a:12,b:[a:1,c:2]]";
Then you could just do this:
def map=Eval.me(s)
but as is, eval tries to resolve b and d as variables which aren't defined in the scope.
I am using cassandra as DB and storing user address as JSON in "address" field of User object.
class User {
private long id;
private String name;
private String address;
}
Then the User row in DB columns as follows
id | name | address
================================
12345| jhon | {"hno":"12-10-46/3","street":"lavella road","city":"begaluru"}
But when I query User object from DB and returned as result of a REST api I see Jackson converting User object to JSON. But I observed that "address" field as string type instead of another JSON object. But I want that "address" should be interpreted as JSON object on client side.
How I could say jackson to convert internal string property to JSON as well?
I think you should use the annotation #JsonRawValue in your address field.
#JsonRawValue: per-property marker that can be used to specify that
the value of property is to be included in serialization ''exactly''
as is, with no escaping or decoration -- useful for embedding
pre-serialized JSON (or whatever data format is being used) in output
https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
It's being treated as a string because it is a string. You'll need to convert it to an object. Not being fluent in Java I managed to find this snippet that uses the javascript engine to eval the string into an object, the same as a browser would do:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object result = engine.eval([Your Address String Variable Here]);
And then result should be an object derived from the string.
Hope this helps.