Skip to content
Advertisement

Convert String to Object gives error due to double quotes at start and end

i have an object that is coming from the third party api. and it is in the form like this :

"{ "type": "object", "properties": {   "hostUrl": {
    "type": "string",
    "description": "hostUrl",   }, }, }"

due to the double quote in the start and the end i am getting error and json parse is also not being removed so kindly tell me how to remove this double quote which has wrapped my object inside it

Advertisement

Answer

try this

const jsonStr =
  '"{ "type": "object", "properties": {  "hostUrl": { "type": "string", "description": "hostUrl",   }, }, }"';

var json = jsonStr
  .substring(1, jsonStr.length - 1)
  .replaceAll("},", "}")
  .replaceAll(" ", "")
  .replaceAll(",}", "}");

json

{
  "type": "object",
  "properties": {
    "hostUrl": {
      "type": "string",
      "description": "hostUrl"
    }
  }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement