Fifth Dimension Dragon
Fifth Dimension Dragon

Reputation: 197

Why does this JSON object return backslashes? And how do I get rid of them?

Why does this JSON object return backslashes?

https://api.etherscan.io/api?module=contract&action=getabi&address=0xf7F14205898b2b8bC008148E591B1060BE7a50Fb&apikey=DFMRS7RGGBAZW3ZDKEQTRVMBCA87JVSQVT

And how do I get rid of them? Furthermore, how do I get rid of the header status code?

I created a javascript fetch object that fetches the URL and parses it then returns the object in react as a string, but this is unusable in the current context.

The JSON object returns such {"status":"1","message":"OK","result":"[{\"constant\":true,\"inputs\":[],\"name\":\"findPercentage\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}]

But I need it without the header and to return it a newline as such...

        "constant": true,
        "inputs": [],
        "name": "name",
        "outputs": [
            {
                "name": "",
                "type": "string"
            }
        ],

Upvotes: 0

Views: 97

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370709

The response is a JSON string which contains 3 properties when parsed: a message, a status, and a result. That result property's value is another JSON string, so you need to parse it as well, and you'll have a well-formed object to work with:

fetch('https://api.etherscan.io/api?module=contract&action=getabi&address=0xf7F14205898b2b8bC008148E591B1060BE7a50Fb&apikey=DFMRS7RGGBAZW3ZDKEQTRVMBCA87JVSQVT')
  .then(res => res.json())
  .then((responseObj) => {
    const trueObj = JSON.parse(responseObj.result);
    console.log(trueObj);
  });

Keep in mind that there's no such thing as a "JSON Object". If you have an object or array, then you have an object or array, full stop. JSON format is a method of representing an object in a string, like const myJSON = '{"foo":"bar"}'.

Upvotes: 1

Related Questions