Remove the double quotes in JSON String created using GSON - java

I got the below result in spark after using GSON library.
[
"{"A":"1","A-Description":"Eastern "}",
"{"B":"2","B-Description":"Western "}",
"{"C":"3","C-Description":"Northern "}",
"{"D":"4","D-Description":"Southern"}"
]
I want to remove the double quotes from start and end of json string
Final result will be as below :
[
{"A":"1","A-Description":"Eastern "},
{"B":"2","B-Description":"Western "},
{"C":"3","C-Description":"Northern "},
{"D":"4","D-Description":"Southern"}
]
I have solved the issue as below :
val jsonString = str.replaceAll("\\\\", "").replaceAll("\"(.+)\"", "$1")
where str is some string.
Please suggest more efficient way if available.

Related

Regex to match key value pairs from JSON

I need to match all the key values pairs from a complex JSON, but only values that are text / String.
For example from:
{"results":[
{"id":16,"name":"some name1","location":"some location1","parent":true, ,"region":"some region"},
{"id":157,"name":"some name2" , "location":some location2","parent":true}
],"totalCount":170}
I need to match:
"name"
"some name1"
"location"
"some location1"
"region"
"some region1"
etc
I have this [^:]+\"(?=[,}\s]|$) , but it only matches the values (which are correct).
I need also to match the keys: "name" , "location", "region" (and there can be other key names)
Here is an example for values matched https://regex101.com/r/m8FePZ/6
As others pointed out, if you want a robust solution use a JSON parser in your language.
If you want to use regex, and the engine supports lookbehind you can use this:
/("[^"]*"(?=:")|(?<=":)"[^"]*")/g
Explanation:
| - or combination of:
"[^"]*"(?=:") - quote, 0+ non-quotes, quote, followed by positive lookahead for colon and quote
(?<=":)"[^"]*" - positive lookbehind for quote and colon, followed by quote, 0+ non-quotes, quote
If you want to exclude the quotes in the matches, use this regex:
/(?<=")([^"]*(?=":")|(?<=":")[^"]*)/g
Note that these regexes fail for cover corner cases, such as whitespace around keys and values, escaped quotes in values, etc. Hence it is safer to use an actual JSON parser.
I saw that you decided not to use regex and used JSON library finally. Here is the simple solution by "Josson & Jossons".
https://github.com/octomix/josson
implementation 'com.octomix.josson:josson:1.3.22'
---------------------------------------------
Josson josson = Josson.fromJsonString(
"{\n" +
" \"results\": [\n" +
" {\n" +
" \"id\": 16,\n" +
" \"name\": \"some name1\",\n" +
" \"location\": \"some location1\",\n" +
" \"parent\": true,\n" +
" \"region\": \"some region\"\n" +
" },\n" +
" {\n" +
" \"id\": 157,\n" +
" \"name\": \"some name2\",\n" +
" \"location\": \"some location2\",\n" +
" \"parent\": true\n" +
" }\n" +
" ],\n" +
" \"totalCount\": 170\n" +
"}");
JsonNode node = josson.getNode(
"results.entries().[value.isText()]*.toArray()");
System.out.println(node.toPrettyString());
Output
[ "name", "some name1", "location", "some location1", "region", "some region", "name", "some name2", "location", "some location2" ]

Changing a string in a jsonArray without adding extra slashes before quotes

I have a JSONArray object and sometimes it has elements that contain \u0000 chars. I'm trying to remove them by converting the element into a string, removing the unwanted chars and adding it back to the JSONArray. The problem is that I get extra slashes before the quotes that I don't want.
Is there any way to manipulate the content of the array without getting those slashes?
My code is like the following (queryArray is my old JSONArray, and I'm trying to create a new one with the cleaned strings):
JSONArray newQueryArray = new JSONArray();
for (Object jsonObj : queryArray){
String sqlValue = jsonObj.toString();
sqlValue = sqlValue.replace("\u0000", "");
sqlValue = sqlValue.replace("\\u0000", "");
sqlValue = sqlValue.replace("\\\"","\"");
newQueryArray.put(sqlValue);
}
I get for example the following JSON:
"query": [
"{\"Line 0\":\"4\"}"
],
while I wish to get:
"query": [
{
"Line 0": "4"
}
],

How add quotes in a JSON string, using Java, when the value is a date

I'm facing difficulties in a scenario that I need to read a JSON object, in Java, that has no double quotes in the keys and no values, like the example below:
"{id: 267107086801, productCode: 02-671070868, lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58, user: {pf: {document: 123456789, name: Luis Fernando}, address: {street: Rua Pref. Josu00e9 Alves Lima,number:37}, payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}"
I was able to add the double quotes in the fields using the code below, with replaceAll and the Gson library:
String jsonString = gson.toJson (obj);
String jsonString = jsonString.replaceAll ("([\\ w] +) [] *:", "\" $ 1 \ ":"); // to quote before: value
jsonString = jsonString.replaceAll (": [] * ([\\ w # \\.] +)", ": \" $ 1 \ ""); // to quote after: value, add special character as needed to the exclusion list in regex
jsonString = jsonString.replaceAll (": [] * \" ([\\ d] +) \ "", ": $ 1"); // to un-quote decimal value
jsonString = jsonString.replaceAll ("\" true \ "", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll ("\" false \ "", "false"); // to un-quote boolean
However, fields with dates are being broken down erroneously, for example:
"{"id" : 267107086801,"productCode" : 02-671070868,"lastUpdate" : 2018-07-15,"lastUpdateTimestamp" : 2018-07-15 "01" : 49 : 58,"user" :{"pf":{"document" : 123456789, "name" : "Luis" Fernando},"address" :{"street" : "Rua"Pref.Josu00e9AlvesLima,"number" : 37},"payment" : [{"sequential" : 1,"id" : "CREDIT_CARD","value" : 188,"installments" : 9}]}"
Also, strings with spaces are wrong as well. How could I correct this logic? What am I doing wrong? Thanks in advance.
String incorrectJson = "{id: 267107086801, productCode: 02-671070868,"
+ " lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58,"
+ " user: {pf: {document: 123456789, name: Luis Fernando},"
+ " address: {street: Rua Pref. Josu00e9 Alves Lima,number:37},"
+ " payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}";
String correctJson = incorrectJson.replaceAll("(?<=: ?)(?![ \\{\\[])(.+?)(?=,|})", "\"$1\"");
System.out.println(correctJson);
Output:
{id: "267107086801", productCode: "02-671070868", lastUpdate:
"2018-07-15", lastUpdateTimestamp: "2018-07-15 01:49:58", user: {pf:
{document: "123456789", name: "Luis Fernando"}, address: {street: "Rua
Pref. Josu00e9 Alves Lima",number:"37"}, payment: [{sequential: "1",
id: "CREDIT_CARD", value: "188", installments: "9"}]}
One downside of non-trivial regular expressions is they can be hard to read. The one I use here matches each literal value (but not values that are objects or arrays). I am using colons, commas and curly braces to guide the matching so I don’t need to care what is inside each string value, it may be any characters (except comma or right curly brace). The parts mean:
(?<=: ?): there’s a colon an optionally a blank before the value (lookbehind)
(?![ \\{\\[]) the value does not start with a blank, curly brace or square bracket (negative lookahead; blank because we don’t want a blank between the colon and the value to be taken as part of the value)
(.+?): the value consists of at least one character, as few as possible (reluctant quantifier; or regex would try to take the rest of the string)
(?=,|}): after the value comes either a comma or a right curly brace (positive lookahead).
Without being well versed in JSON I don’t think you need to quote the name. You may, though:
String correctJson = incorrectJson.replaceAll(
"(?<=\\{|, ?)([a-zA-Z]+?): ?(?![ \\{\\[])(.+?)(?=,|})", "\"$1\": \"$2\"");
{"id": "267107086801", "productCode": "02-671070868", "lastUpdate":
"2018-07-15", "lastUpdateTimestamp": "2018-07-15 01:49:58", user: {pf:
{"document": "123456789", "name": "Luis Fernando"}, address:
{"street": "Rua Pref. Josu00e9 Alves Lima","number": "37"}, payment:
[{"sequential": "1", "id": "CREDIT_CARD", "value": "188",
"installments": "9"}]}
The following code takes care single quote present in JSON string as well as a key containing number
jsonString = jsonString.replaceAll(" :",":"); // to trip space after key
jsonString = jsonString.replaceAll(": ,",":,");
jsonString = jsonString.replaceAll("(?<=: ?)(?![ \{\[])(.+?)(?=,|})", ""$1"");
jsonString = jsonString.replaceAll("(?<=\{|, ?)([a-zA-Z0-9]+?)(?=:)",""$1"");
jsonString = jsonString.replaceAll(""true"", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll(""false"", "false"); // to un-quote boolean
jsonString = jsonString.replaceAll(""null"", "null");// to un-quote null
jsonString = jsonString.replaceAll(":",", ":"" ,"); // to remove unnecessary double quotes
jsonString = jsonString.replaceAll("true"", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll("'",", "',"); // to handle single quote within json string
jsonString = jsonString.replaceAll("'},", "'}","); // to put double quote after string ending with single quote

Java: Gson parse without square brackets

I have a JSON file that I'm trying to parse that doesn't have surrounding square brackets: [...].
Here is the Java code. (This has the Gson dependency in Maven) I keep on getting the error on line 29, "This is not a JSON Array." However, when I add square brackets to the beginning and end of the JSON file, it parses just fine. How can I parse this without the brackets.
Here is the JSON:
{
"title": "Title",
"content": "whatever",
"author": "Yuki Noguchi",
"date_published": "2017-02-15T20:25:00.000Z",
"lead_image_url": "https://media.npr.org/assets/img/2017/02/15/ap_17039860769171_wide-d1a5d3c17f00d78fd1df9d19a96e1d7b3bd38e60.jpg?s=1400",
"dek": null,
"next_page_url": null,
"url": "http://www.npr.org/2017/02/15/515425370/trump-labor-pick-andrew-puzders-nomination-appears-in-jeopardy",
"domain": "www.npr.org",
"excerpt": "The fast-food CEO faced fierce opposition from labor groups, plus personal controversies. Ultimately, he didn't have support from enough Republican senators.",
"word_count": 751,
"direction": "ltr",
"total_pages": 1,
"rendered_pages": 1
}
Thanks!
Try this one, append "[" open and "]" close braces and store in temporary variable and pass that temporary variable to parse.
for example,
var jsonVar = { name:"hello", title: "WOrld" };
var tmpJsonVar = "[" + jsonVar + "]";
var jsonObject = JSON.parse(tmpJsonVar);

GSON : How to convert Object (which itself contains JSON) to JSON

I have a different use case where fields in POJO itself stores JSON data, basically grouping of all data.
Now i want the complete JSON generated from above POJO.
At present i am using this method
private static Gson gson = new Gson();
public static String convertObjectToJSON(Object object) throws Exception {
String jsonResponse = gson.toJson(object);
return jsonResponse;
}
But getting output with escape characters around double quotes like below
{ \"_id\" : 234242, \"name\" : \"carlos\"}
I tried various options in GsonBuilder, but not working.
Basically, i am just grouping multiple JSON data and sending it across.
Could you please do the needful help to get rid of the escape characters around double quotes.
UPDATE:
Question is : I have 3 JSONs and need to combine them into a single JSON and need to pass it to html5 client through Spring MVC. As of now i am added the 3 JSONs into a POJO and trying to convert the same to JSON. Same is explained above.
Thanks & Regards
Venkat
I have tried below sample code and it doesn't have any escape characters around double quotes.
class MyPOJO{
private int _id;
private String name;
// getter & setter
}
String json="{ \"_id\" : 234242, \"name\" : \"carlos\"}";
MyPOJOobj=new Gson().fromJson(json, MyPOJO.class);
System.out.println(new Gson().toJson(obj));
output:
{"_id":234242,"name":"carlos"}
EDIT
If you want to combine 3 JSON string then It will be stored as List of object as illustrated below:
sample code:
String json = "[" +
" { \"_id\" : 1, \"name\" : \"A\"}," +
" { \"_id\" : 2, \"name\" : \"B\"}," +
" { \"_id\" : 3, \"name\" : \"C\"}" +
"]";
Type type = new TypeToken<ArrayList<MyPOJO>>() {}.getType();
ArrayList<MyPOJO> obj = new Gson().fromJson(json, type);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));
output:
[
{
"_id": 1,
"name": "A"
},
{
"_id": 2,
"name": "B"
},
{
"_id": 3,
"name": "C"
}
]

Categories