Ad

thx!

Code
Diff
  • function toPrimitiveDeep(stringObject){
      let result
      try{
        if (stringObject==='undefined') return undefined // :)
        result = JSON.parse(stringObject);
      }catch (e){
        console.log(`error`,e)
        return stringObject
      }
      if(typeof result !== 'object' || result===null) return result
      let keys = Object.keys(result)
      keys.forEach((key)=>{
        result[key] = toPrimitiveDeep(result[key])
      })
      return result
    }
    • function toPrimitiveDeep(stringObject){
    • let result
    • try{
    • if (stringObject==='undefined') return undefined // :)
    • result = JSON.parse(stringObject);
    • }catch (e){
    • console.log(`error`,e)
    • return stringObject
    • }
    • if(typeof result !== 'object') return result
    • if(typeof result !== 'object' || result===null) return result
    • let keys = Object.keys(result)
    • keys.forEach((key)=>{
    • result[key] = toPrimitiveDeep(result[key])
    • })
    • return result
    • }

given that you have:

stringObject = `{
  \"a\":\"false\",
  \"b\":\"33\",
  \"c\":\"hi i'm C!\",
  \"d\":\"{
    \\\"d1\\\":\\\"4.5\\\",
    \\\"d2\\\":\\\"Yo D2 here\\\"
  }\"
}`

make it deeply primitive as such:

resultObject = {
  a: false,
  b: 33,
  c: "hi i'm C!",
  d:{
    d1:4.5,
    d2:"Yo D2 here"
  }
}
function toPrimitiveDeep(stringObject){
  let result
  try{
    result = JSON.parse(stringObject);
  }catch (e){
    return stringObject
  }
  if(typeof result !== 'object') return result
  let keys = Object.keys(result)
  keys.forEach((key)=>{
    result[key] = toPrimitiveDeep(result[key])
  })
  return result
}