0

I have string like:

String metadata = "{ Item: {Owner:John, About:{website:www.john.com/about, firstName:William, lastName:Shakespeare, date:1/2/2000, books:[Othello, R&J], shelf:[12/3/4, 14/5/6, 17/8/6]}}}"

I want to convert this metadata into JSON format. But because of missing quotes I was not able to convert it.

I can write a code that could do parsing and add quotes, that does not becomes flexible for any string to make it into JSON. (Since this metadata is prone to changes in future).

Is there a way to add quotes to any string to make it into JSON?

Or how can be this generalized so that simple string is converted into JSON. Do we have any library in Java that converts?

Code snippets will be really helpful to understand, if no library is there.

Well, I prefer not to use any external library.

NoNaMe
  • 6,020
  • 30
  • 82
  • 110
Shubham
  • 33
  • 12
  • You could simply parse it into an object that represents the metadata. Converting a (simple) object into JSON is trivial with Jackson, for example. – Florian Schaetz Dec 01 '15 at 06:57
  • 1
    "I have a string" is not very solid explanation of format. Generally parsing text that was not designed to handle arbitrary values like `{Owner=Bob,The Great, About={home=Me=my}}` is painful, but not hard if you willing to ignore escaping issues... Note that searching for libraries is off-topic on SO and "write parser for me" likely will be considered too broad. – Alexei Levenkov Dec 01 '15 at 06:59
  • Possible duplicate of [Converting JSON to Java](http://stackoverflow.com/questions/1688099/converting-json-to-java) – Ali Dec 01 '15 at 07:01

2 Answers2

0

Your string seems to use fancy incoherent notation: colons and equal signs to separate keys, unquoted strings, dates with multiple formats...

How is it generated in the first place? It may be easier to change how that string is generated than parsing it again.

To parse the string, you must first determine the rules on how to break it apart (the grammar). Incoherent as it is, this parser would have no end of special cases.

cadrian
  • 7,332
  • 2
  • 33
  • 42
  • I did not watch out for the colons and equals while typing the format. I have resolved. It is an incoming data as a string. I thought of converting into JSON to access the parts of string to get relevant information. – Shubham Dec 01 '15 at 07:09
  • I want to know if there is any general regular expression way to place quotes at appropriate places. Then I can use a `new JSONObject(String)` to convert the string into JSON. – Shubham Dec 01 '15 at 07:42
0
public static void main(String[] args){
        String metadata = "{ Item: {Owner:John, About:{website:www.john.com/about, firstName:William, lastName:Shakespeare, date:1/2/2000, books:[Othello, R&J], shelf:[12/3/4, 14/5/6, 17/8/6]}}}";
        String json = "";
        StringTokenizer st = new StringTokenizer(metadata, "([^:{}, ])", true);
        StringTokenizer stkey = new StringTokenizer(metadata, "([^:{}, ])", false);
        while(stkey.hasMoreTokens()){
            String s1 = stkey.nextToken();
            while(st.hasMoreTokens()){
                String s2 = st.nextToken();
                if(s1.equals(s2)){
                    json += "\"" + s2.trim() + "\"";
                    break;
                } else {
                    json += s2;
                }
            }
        }
        while(st.hasMoreTokens()){
            json += st.nextToken();
        }
        System.out.println(json);
        JSONObject jo = JSONObject.fromObject(json);
    }

Only can regarded every values as string. you can control regex repression to do better.

fmyblack
  • 83
  • 2
  • 10