1

I have a string created from a JSON object as follows

{
 "id" : "112233",
 "someElement" : [ {
     "map" : {
        "123" : [ {..}]
      }, 
     "map" : {
        "124" :[ {..}]
      } 
  }]
} 

I need to convert the above string to the following format.

{
  "id" : "112233",
 "someElement" : [ {
     "123" : {
        "element" : [ {..}]
      }, 
     "124" : {
        "element" :[ {..}]
      } 
  }]
} 

I tried to do string substitution as when the substring "map" is found in the string, replace with the ID just beneath it.

String a = jsonString.substring(jsonString.indexOf("map")+16, jsonString.indexOf("map")+19);
String b = jsonString.replace("map", a);

This pattern works for the first occurrence of "map" string. But the same ID value replaces the second "map" string. How do I replace the subsequent occurrences of "map" string with their respective IDs.

Also, is there any better way to do this? Appreciate any feedback.Thanks!

mona
  • 6,079
  • 12
  • 41
  • 46
  • Is it a valid json string? A JSON string can't have repeated keys such as "map" in this case. If you correct it then I can help you. – Braj Jul 16 '14 at 18:23
  • I just tested it in http://jsonlint.com website. And it says its a valid JSON. Thanks – mona Jul 16 '14 at 18:27
  • I know it's valid but you can't convert it into Object if needed. By the way what are you doing with this JSON string. Read [Do JSON keys need to be unique?](http://stackoverflow.com/questions/5306741/do-json-keys-need-to-be-unique) – Braj Jul 16 '14 at 18:28
  • Yes, that is true. But its a requirement. This JSON is intended to be a serialization of data, and not an object model. Hence I going down this route.. – mona Jul 16 '14 at 18:30
  • OK then I can't help you. Read the link and Good luck. – Braj Jul 16 '14 at 18:30
  • The specification says names *"SHOULD"* be unique, not *"MUST".* It then points out that the behavior when this recommendation is ignored will be implementation-specific. – erickson Jul 16 '14 at 18:32

1 Answers1

2

JSON is not a regular language, so trying to make this kind of change with a regular expression will be fragile; syntactically insignificant variations in the input will easily confuse your regular-expression–based solution.

Because this example violates the JSON recommendation for keeping object member names unique, many JSON parsers will have difficulty parsing it, raising an exception or ignoring some members. However, there might be parsers out there that handle it. If not, it's very easy write your own parser for JSON that will handle this input robustly. Then your code won't break when the whitespace changes.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • Thanks for the feedback. I am aware that this is not the best way to handle a JSON output, but I need it as a requirement. This JSON is intended to be a serialization of data, and not an object model.So I was trying to do some string operations to get my desired result. But since it has repetitive string, I am not able to work it out. – mona Jul 16 '14 at 18:35
  • @mona You don't have to bind the data to a real object model. Simple parsers will represent JSON as a nested structure of collections and values. That would be sufficient to rearrange some of the elements and then reserialize. – erickson Jul 16 '14 at 18:39