Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i add double quotes to look like json

Tags:

java

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

like image 549
Musaddique Avatar asked Apr 08 '16 07:04

Musaddique


People also ask

How do you represent double quotes in JSON?

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.

Does JSON accept double quotes?

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 .

How can you use a double quote inside a JSON string?

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.

Is JSON double or single quotes?

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 .


2 Answers

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);
like image 188
Mahendra Avatar answered Sep 29 '22 10:09

Mahendra


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();
like image 34
nyname00 Avatar answered Sep 29 '22 11:09

nyname00