Recover Json Data with Javascript

Asked

Viewed 166 times

0

I am retrieving a Json by Node/Axios and receive the following structure (A and B). I have 2 occurrences of establishments.

RESULT TO

{
  statuscod: 200,
  mensagem: null,
  dados: '{"estrutura":{"estabelecimentos":[{"idPropriedade":353359,"municipio"...
} 

result B

Json result of the "data variable"

    {
    "estrutura": {
        "estabelecimentos": [{
            "idPropriedade": 353359,
            "municipio": "Votuporanga",
            "uf": "SP",
        }, {
            "idPropriedade": 353366,
            "municipio": "Valentim Gentil",
            "uf": "SP",
        }],
        "qtde": 2
    }
}

In the JS running console.log(Response.data) it shows the result B, but I can’t access the most internal data (Uf of the establishments, Qtde for example) I have already executed: data.GtaEstablishmentsWSVO.establishments[0]. Uf data.GtaEstablishingcementsWSVO.Qtde but without success. Could you help me by telling me what’s wrong? Accessing.statuscod data returns 200 (correct)

  • Where Gtaestabelecimentoswsvo is written, read structure

1 answer

3


Good night ! First I would like to comment that the next times I ask a question here it would be interesting to show the code that is running and the errors generated during execution because it makes it easy for everyone to find error and it will certainly make you get an accurate answer much faster :) !

But anyway one possibility is that response.dados despite having the structure of a JSON is actually a string. Logo response.dados.qtde does not make sense because a string is basically a list of characters. You can test this hypothesis by doing console.log(response.dados[0]) if this prints on the screen a character you are sure that this variable is a string.

If this is the case, all you need to do is parse the string for JSON object:

obj = JSON.parse(response.dados)
console.log(obj.qtde) 

You can get more details about JSON.parse here

  • Lucas, thank you for your attention and help. That’s exactly what you reported. Problem solved. Hugs.

  • @Remember to mark the answer as accepted if she solved and explained the problem you had.

Browser other questions tagged

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