0

I'm using axios to call APIs I received an API without patterns, and the return is:

{
    "pedidos": [
        {
            "cnpj": "09922334480001",
            "nr-pedido": "1234567",
            "os-cliente": "811",
            "data-entrada": "2020-08-03",
            "previsao-entrega": "2020-09-28",
            "status": "Em processo"
        }
    ]
}

When i parse JSON i use:

result.data.pedidos[0].status;

but when i have "-" in fields how can i call? When i use:

 result.data.pedidos[0].previsao-entrega 

the error is returned.

Anyone can help me?

3 Answers3

2

For keys with "-" and other characters that won't work with dot (.) notation, use the bracket ([ and ]) notation instead, and enclose the key in quotation marks to make it a string:

result.data.pedidos[0]["data-entrada"]
Trott
  • 66,479
  • 23
  • 173
  • 212
1

result.data.pedidos[0]["previsao-entrega"]

ajvg94
  • 112
  • 1
  • 7
1

Like so:

result.data.pedidos[0]["previsao-entrega"]

See here:

https://stackoverflow.com/a/13869651/12485722

Eduards
  • 1,734
  • 2
  • 12
  • 37