askwer
askwer

Reputation: 235

How do I convert this JSON string enclosed in double quotes to object

My application receives an encrypted json from front end, I decrypt it on backend but it is formatted like this:

"{\"firstname\":\"JOHN\",\"lastname\":\"DOE\"}"

yes, there are leading and trailing double quotes on the decrypted json.

GSON and object mapper doesnt work since they detect the quotes on the first character.

Is there way to properly convert this without manually removing the enclosing quotes?

Upvotes: 1

Views: 1979

Answers (1)

Aasmund Eldhuset
Aasmund Eldhuset

Reputation: 37950

It looks like your data has been double-encoded, so the solution is to double-decode. Specifically: thanks to the backslashes, this is actually a valid JSON string value that represents a stringified JSON object. So if you ask your JSON library to decode this into a String, you should get a string whose value is

{"firstname":"JOHN","lastname":"DOE"}

And then you can ask your JSON library to decode that string into the type you actually want.

Upvotes: 2

Related Questions