Repetition with Json in JS

Asked

Viewed 33 times

-1

Hello, I am new to programming and am learning to use JSON with JS. This is the JSON I’m using:

{
  "USD": {
    "code": "USD",
    "codein": "BRL",
    "name": "Dólar Comercial",
    "high": "5.564",
    "low": "5.4489",
    "varBid": "-0.0702",
    "pctChange": "-1.26",
    "bid": "5.49",
    "ask": "5.4905",
    "timestamp": "1616187593",
    "create_date": "2021-03-19 21:00:02"
  },
  "USDT": {
    "code": "USD",
    "codein": "BRLT",
    "name": "Dólar Turismo",
    "high": "5.7",
    "low": "5.585",
    "varBid": "-0.075",
    "pctChange": "-1.32",
    "bid": "5.46",
    "ask": "5.79",
    "timestamp": "1616180760",
    "create_date": "2021-03-19 21:00:04"
  },
  "CAD": {
    "code": "CAD",
    "codein": "BRL",
    "name": "Dólar Canadense",
    "high": "4.463",
    "low": "4.3567",
    "varBid": "-0.0587",
    "pctChange": "-1.32",
    "bid": "4.3918",
    "ask": "4.3959",
    "timestamp": "1616187598",
    "create_date": "2021-03-19 21:00:02"
  },
  "EUR": {
    "code": "EUR",
    "codein": "BRL",
    "name": "Euro",
    "high": "6.6379",
    "low": "6.487",
    "varBid": "-0.0887",
    "pctChange": "-1.34",
    "bid": "6.5339",
    "ask": "6.5391",
    "timestamp": "1616187597",
    "create_date": "2021-03-19 21:00:01"
  },
  "GBP": {
    "code": "GBP",
    "codein": "BRL",
    "name": "Libra Esterlina",
    "high": "7.7619",
    "low": "7.5623",
    "varBid": "-0.124",
    "pctChange": "-1.6",
    "bid": "7.6126",
    "ask": "7.6195",
    "timestamp": "1616187598",
    "create_date": "2021-03-19 21:00:03"
  },
  "ARS": {
    "code": "ARS",
    "codein": "BRL",
    "name": "Peso Argentino",
    "high": "0.061",
    "low": "0.0597",
    "varBid": "-0.0008",
    "pctChange": "-1.23",
    "bid": "0.0601",
    "ask": "0.0602",
    "timestamp": "1616187593",
    "create_date": "2021-03-19 21:00:03"
  },
  "BTC": {
    "code": "BTC",
    "codein": "BRL",
    "name": "Bitcoin",
    "high": "330100",
    "low": "315000",
    "varBid": "-7701.5",
    "pctChange": "-2.34",
    "bid": "320650",
    "ask": "321798.5",
    "timestamp": "1616351538",
    "create_date": "2021-03-21 15:32:19"
  },
  "LTC": {
    "code": "LTC",
    "codein": "BRL",
    "name": "Litecoin",
    "high": "1136",
    "low": "1090",
    "varBid": "-26.6",
    "pctChange": "-2.34",
    "bid": "1102.54",
    "ask": "1103",
    "timestamp": "1616351499",
    "create_date": "2021-03-21 15:31:39"
  },
  "JPY": {
    "code": "JPY",
    "codein": "BRL",
    "name": "Iene Japonês",
    "high": "0.05121",
    "low": "0.05005",
    "varBid": "-0.0007",
    "pctChange": "-1.37",
    "bid": "0.05038",
    "ask": "0.05044",
    "timestamp": "1616187585",
    "create_date": "2021-03-19 21:00:03"
  },
  "CHF": {
    "code": "CHF",
    "codein": "BRL",
    "name": "Franco Suíço",
    "high": "6.0107",
    "low": "5.8584",
    "varBid": "-0.0852",
    "pctChange": "-1.42",
    "bid": "5.9101",
    "ask": "5.9122",
    "timestamp": "1616187597",
    "create_date": "2021-03-19 21:00:03"
  },
  "AUD": {
    "code": "AUD",
    "codein": "BRL",
    "name": "Dólar Australiano",
    "high": "4.3219",
    "low": "4.224",
    "varBid": "-0.0609",
    "pctChange": "-1.41",
    "bid": "4.2498",
    "ask": "4.2535",
    "timestamp": "1616187598",
    "create_date": "2021-03-19 21:00:02"
  },
  "CNY": {
    "code": "CNY",
    "codein": "BRL",
    "name": "Yuan Chinês",
    "high": "0.8552",
    "low": "0.8373",
    "varBid": "-0.0113",
    "pctChange": "-1.32",
    "bid": "0.8435",
    "ask": "0.844",
    "timestamp": "1616187544",
    "create_date": "2021-03-19 17:59:04"
  },
  "ILS": {
    "code": "ILS",
    "codein": "BRL",
    "name": "Novo Shekel Israelense",
    "high": "1.6964",
    "low": "1.6508",
    "varBid": "-0.0273",
    "pctChange": "-1.61",
    "bid": "1.6633",
    "ask": "1.6644",
    "timestamp": "1616187546",
    "create_date": "2021-03-19 17:59:06"
  },
  "ETH": {
    "code": "ETH",
    "codein": "BRL",
    "name": "Ethereum",
    "high": "1200.21",
    "low": "1200.21",
    "varBid": "0",
    "pctChange": "0",
    "bid": "1440.25",
    "ask": "16999.75",
    "timestamp": "1610415652",
    "create_date": "2021-01-11 22:40:52"
  },
  "XRP": {
    "code": "XRP",
    "codein": "BRL",
    "name": "Ripple",
    "high": "3.1",
    "low": "2.78",
    "varBid": "-0.08",
    "pctChange": "-2.78",
    "bid": "2.81",
    "ask": "2.83",
    "timestamp": "1616351538",
    "create_date": "2021-03-21 15:32:18"
  }
}

Source: https://economia.awesomeapi.com.br/json/all

Code:

let data = fazGet("https://economia.awesomeapi.com.br/json/all");
    let moeda = JSON.parse(data);
    //console.log(moedas);
    document.getElementById("p").innerHTML += moeda.ARS["name"];
    document.getElementById("p").innerHTML += moeda.BTC["name"];  
    document.getElementById("p").innerHTML += moeda.AUD["name"];

How can I do not always have to put the name of the currency? Use a variable in the case. Example:

document.getElementById("p").innerHTML += moeda.variavel["name"];
                                                ↑↑↑↑↑↑↑↑

since in this challenge I have a json inside another json. I have tried with moeda[0]/moeda.[0]

  • what you want to do?

1 answer

3


From what I understand you want to take all the coins and go doing automatically without having to explain a moeda.ARS, I think that would solve your problem:

    const data = await fetch("https://economia.awesomeapi.com.br/json/all")
    const jsonData = await data.json()
    // esse Object.keys te traz todas as chaves em formato de array
    const keys = Object.keys(jsonData)
    keys.forEach(key => {
         document.getElementById("p").innerHTML += jsonData[key]["name"];
    })
  • 1

    Thank you, that solved everything!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.