#JSON.parse() error in typescript

13 messages · Page 1 of 1 (latest)

dusky prawn
#

I'm trying JSON.parse() but faced an unexpected error. Can you help me now?
const abi = "[{\"type\": \"function\", \"name\": \"empty\", \"inputs\": [], \"outputs\": [], \"state_mutability\": \"external\"}]"
console.log(JSON.parse(abi))

I also tried JSON.parse(abi.replace(/\\"/g, '\"')) but failed

plush cape
#

@dusky prawn Seems to work okay.

south skyBOT
#
Ascor8522#7606

Preview:```ts
const abi = [ { "type": "function", "name": "empty", "inputs": [], "outputs": [], "state_mutability": "external" } ]

console.log(JSON.parse(abi))```

dusky prawn
#

thanks but abi should not be changed

south skyBOT
#
allred4#2988

Preview:```ts
const abi =
'[{\"type\": \"function\", \"name\": \"empty\", \"inputs\": [], \"outputs\": [], \"state_mutability\": \"external\"}]'
const abiJson = JSON.parse(
abi.replace(/\\\"/g, '\"')
)

console.log(abiJson)```

orchid cargo
#

you don't need a triple slash

#

that's the problem

dusky prawn
#

make sense but it comes from server side.

orchid cargo
#

replace all tripple slashes with single slashes

#

or fix the server

plush cape
#

Yeah, slashes are only for escaping the JS literal strings. (Though other backend languages might need them to escape their literal strings)

#

The actual string being JSON parsed should not have slashes in it.