1

I have a json in the below format which was shared by a different team. I need to manipulate this JSON for one of my requirements.

JSON format which I received:

"{""event"":{""signup"":false,""zip"":""1985"",""type"":""DIY"",""registration"":false,""file"":{""filePath"":[""9a16-832c6e90dc47/5fa0ee/163221822779.png""]},""details"":{""dream"":""test""}},""slot"":{""loc"":{""type"":""NO""},""rest"":""No""}}"

The actual format is supposed to be in this for converting the string to JSON object.

{
  "event": {
    "signup": false,
    "zip": "1985",
    "type": "DIY",
    "registration": false,
    "file": {
      "filePath": [
        "9a16-832c6e90dc47/5fa0ee/163221822779.png"
      ]
    },
    "details": {
      "dream": "test"
    }
  },
  "slot": {
    "loc": {
      "type": "NO"
    },
    "rest": "No"
  }
}

I'm trying to do something like this for parsing using couchbase SDK.

JsonTranscoder trans = new JsonTranscoder();
om.couchbase.client.java.document.json.JsonObject jsonObj = trans.stringToJsonObject(actualJson);

I'm getting the below exception while parsing, as it treats the JSON string as an invalid one

java.lang.IllegalStateException: Expecting Object as root level object, was: VALUE_STRING

Since it is not in my control, how can the below format be parsed to a JSON object at runtime using java?

  "{""event"":{""signup"":false,""zip"":""1985"",""type"":""DIY"",""registration"":false,""file"":{""filePath"":[""9a16-832c6e90dc47/5fa0ee/163221822779.png""]},""details"":{""dream"":""test""}},""slot"":{""loc"":{""type"":""NO""},""rest"":""No""}}"
Aishu
  • 1,310
  • 6
  • 28
  • 52
  • 6
    You need to tell the other team to properly generate valid JSON as it is not in its current state (mind the duplicated `"`). Don't workaround basic issues but strive to fix them at their source. – João Dias Oct 24 '21 at 13:13
  • 2
    Sorry @Aishu, but I think Joao has the best answer here. Back-to-back quotes to escape the quotes is a problem. I can't think of any standard way to decode that. You could try replacing "" with " in your string, but I think that's just going to lead to other problems. I think you should let them know that the JSON they're producing isn't correct (first double check to make sure it isn't your own code that's adding the extra quotes, of course) – Matthew Groves Oct 25 '21 at 13:39
  • sure @MatthewGroves – Aishu Oct 25 '21 at 14:03

0 Answers0