Accessing a JSON with JS

Asked

Viewed 298 times

0

I was trying to access a JSON with JS, here’s a example how they are:

{
  status: "0",
  ano: "Não Informado",
  competencia: "Não Informado",
  id_Publication: "8"
}

But actually this is only one part of the full JSON. This is "a multidimensional array". How can I access information within p, more specifically the part of competence?

I’ve tried using that code:

{
  targets: 7,
  data: "",
  render: function(data, type, full){
    returnconsole.log(full);
  }
}

It returns all the JSON if use this way and put:

{
  targets: 7,
  data: "",
  render: function(data, type, full){
    returnconsole.log(full['p']);
  }
}

It returns the data only from p, BUT if I try to put:

{
  targets: 7,
  data: "",
  render: function(data, type, full){
    returnconsole.log(full['p.competencia']);
  }
}

He returns Undefined.

  • This JSON is not valid. It corrects the same so that we can answer, it is very confused, validates it here: https://jsonformatter.curiousconcept.com/

  • It is valid yes, it works here, this is an example, how can I access it? @Leonardobonetti

  • Now yes is valid. I will post the answer !

1 answer

1

Just create an object from your JSON myObj ={json aqui} and access by object name point attribute: myObj.competencia. Simple, easy and fast.

myObj = {status: "0", ano: "Não Informado", competencia: "Não Informado", id_Publication: "8"};

console.log(myObj.competencia)

Browser other questions tagged

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