i am having below string but i want to add double quotes in it to look like json
[
{
LastName=abc,
FirstName=xyz,
[email protected],
IncludeInEmails=false
},
{
LastName=mno,
FirstName=pqr,
[email protected],
IncludeInEmails=true
}
]
i want below output.
[
{
"LastName"="abc",
"FirstName"="xyz",
"EmailAddress"="[email protected]",
"IncludeInEmails"=false
},
{
"LastName"="mno",
"FirstName"="pqr",
"EmailAddress"="[email protected]",
"IncludeInEmails"=true
}
]
i have tried some string regex. but didn't got. could any one please help.
String text= jsonString.replaceAll("[^\\{\\},]+", "\"$0\"");
System.out.println(text);
thanks
If you're making a . json text file/stream and importing the data from there then the main stream answer of just one backslash before the double quotes: \" is the one you're looking for.
There are three basic types of data that can be represented in JSON, which are strings, numbers, and booleans. Strings should always use double quotes: "this is a string" . Numbers can be any valid Javascript number, like 34 or -0.12 .
Can you use a double quote inside a JSON string? If you use double quotation marks, you do not need to escape single quotation marks embedded in the JSON string. However, you need to escape each double quotation mark with a backtick ` within the JSON structure, as with the following example.
Strings in JSON are specified using double quotes, i.e., " . If the strings are enclosed using single quotes, then the JSON is an invalid JSON .
The regex way, similar to you have tried:
String jsonString = "[ \n" + "{ \n" + " LastName=abc, \n" + " FirstName=xyz, \n"
+ " [email protected], \n" + " IncludeInEmails=false \n" + "}, \n" + "{ \n"
+ " LastName=mno, \n" + " FirstName=pqr, \n" + " [email protected], \n" + " Number=123, \n"
+ " IncludeInEmails=true \n" + "} \n" + "] \n";
System.out.println("Before:\n" + jsonString);
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
System.out.println("===============================");
System.out.println("After:\n" + jsonString);
Since there are a lot of corner cases, like character escaping, booleans, numbers, ... a simple regex won't do.
You could split the input string by newline and then handle each key-value-pair separately
for (String line : input.split("\\R")) {
// split by "=" and handle key and value
}
But again, you will have to handle char. escaping, booleans, ... (and btw, =
is not a valid JSON key-value separator, only :
is).
I'd suggest using GSON since it provides lenient
parsing. Using Maven
you can add it to your project with this dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.6.2</version>
</dependency>
You can then parse your input
string using
String output = new JsonParser()
.parse(input)
.toString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With