0

How do I retrieve the 'name' and 'description' from the JSON data below? I have tried in my code (see below) but couldn't figure it out. Do I use an array to retrieve the name and description from the JSON data?

{
    "status": {
    "timestamp": "2020-10-14T09:04:56.382Z",
    "error_code": 0,
    "error_message": null,
    "elapsed": 8,
    "credit_count": 1,
    "notice": null
    },
    "data": {
    "1": {
        "id": 1,
        "name": "Bitcoin",
        "symbol": "BTC",
        "category": "coin",
        "description": "Bitcoin (BTC) is a cryptocurrency . Users are able to generate BTC through the process of mining. Bitcoin has a current supply of 18,516,718. The last known price of Bitcoin is 11,440.13537699 USD and is down -0.18 over the last 24 hours. It is currently trading on 9569 active market(s) with $22,842,507,797.43 traded over the last 24 hours. More information can be found at https://bitcoin.org/.",
        "slug": "bitcoin",
        "logo": "https://s2.coinmarketcap.com/static/img/coins/64x64/1.png",
        "subreddit": "bitcoin",
        "notice": "",
        "tags": [
        "mineable",
        "pow",
        "sha-256",
        "store-of-value",
        "state-channels"
        ],
        "tag-names": [
        "Mineable",
        "PoW",
        "SHA-256",
        "Store of Value",
        "State channels"
        ],
        "tag-groups": [
        "OTHER",
        "CONSENSUS_ALGORITHM",
        "CONSENSUS_ALGORITHM",
        "PROPERTY",
        "PROPERTY"
        ],
        "urls": {
        "website": [
            "https://bitcoin.org/"
        ],
        "twitter": [],
        "message_board": [
            "https://bitcointalk.org"
        ],
        "chat": [],
        "explorer": [
            "https://blockchain.coinmarketcap.com/chain/bitcoin",
            "https://blockchain.info/",
            "https://live.blockcypher.com/btc/",
            "https://blockchair.com/bitcoin",
            "https://explorer.viabtc.com/btc"
        ],
        "reddit": [
            "https://reddit.com/r/bitcoin"
        ],
        "technical_doc": [
            "https://bitcoin.org/bitcoin.pdf"
        ],
        "source_code": [
            "https://github.com/bitcoin/"
        ],
        "announcement": []
        },
        "platform": null,
        "date_added": "2013-04-28T00:00:00.000Z",
        "twitter_username": "",
        "is_hidden": 0
    }
    }
}
import React, { useEffect, useState } from 'react';
import Axios from 'axios'
import { Link } from 'react-router-dom';
function DetailsScreen(props){ 
    const [cryptoVar, setcryptoList] = useState({name: "", description: ""});
    useEffect(() => {
        Axios.get(`http://localhost:5000/api/getProducts/${props.match.params.id}`, 
        {  
        }).then((response)=>{ 
            console.log(response.data.data ); 
            setcryptoList({
                name: response.data.data,
                description: response.data.data
            });
        }).catch(err =>{
            console.log(err.response.data);
        })
        return () => {
        }
    }, [])
    
    console.log(cryptoVar.description);
    return(
        <div> 
            Name: {cryptoVar.name}
                Description: {cryptoVar.description}
            
            </div> 
    );
}

export default DetailsScreen;
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    Have you tried setcryptoList({ name: response.data.data.1.name – Lazar Nikolic Oct 14 '20 at 09:38
  • 1
    @LazarNikolic you can't use dot notation with a prop whose name starts with (or just *is*) a number – jonrsharpe Oct 14 '20 at 09:39
  • 1
    then this: setcryptoList({ name: response.data.data[1].name – Lazar Nikolic Oct 14 '20 at 09:39
  • 1
    Does this answer your question? [Can I get a javascript object property name that starts with a number?](https://stackoverflow.com/questions/5809790/can-i-get-a-javascript-object-property-name-that-starts-with-a-number) – jonrsharpe Oct 14 '20 at 09:40
  • Yeah, now it works. Thanks guys! That number '1' threw me off. By the way, is this not a proper question to ask on stack overflow? – user10995546 Oct 14 '20 at 09:42
  • 2
    It's not a question asked _well_ for SO. You should do basic debugging to cut your problems down to a [mre] - React, Axios and JSON are irrelevant here, `const data = { 1: "foo" }` then showing `data.1` plus the resulting syntax error would be sufficient, then from there you could even have worked out the appropriate thing to search for yourself. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ is a great starting point. – jonrsharpe Oct 14 '20 at 11:30

0 Answers0