Check values within a JSON

Asked

Viewed 558 times

2

In Javascript I’m getting a JSON like this:

function checaEad(){
    var urlEad = 'http://urljason.json';
    $.getJSON(urlEad, function(data) {
        console.log(data);
    });
}

He returns it to me:

{"user_cpf":"111.111.111-11","user_name":"NOME","matriculas":{
    "357847":{"mat_id":357847,"id_curso":2129,"concluido":true},
    "358696":{"mat_id":358696,"id_curso":4738,"concluido":true},
    "362953":{"mat_id":362953,"id_curso":5041,"concluido":false}
}}

Now I’m unable to handle this array.

I need to check if inside the array that returned to me exists the id_curso = xxxx, if any, check whether concluido = true.

I tried something like this:

data.includes('5041');

But it says that the token is invalid, it is not for me to treat the same if it is a simple array? Because it works like this:

[1, 2, 3].includes(2);     // true
  • Understand the JSON syntax is fundamental to know how to manipulate it. Basically, if it is bounded by { and } is an object. To be an array, the delimiter must be [ and ].

  • And complementing tbm, the object you will access its properties using a . and in the array usually via a index.

  • your check is whether the conclusion is == true or is to change the conclusion to =true ??? was in that doubt?

2 answers

6


This JSON is not an array but an Object. I agree that matriculas could/should be an array... but it is not :)

If you need to check the course status you can do so:

function verificarCursoConcluido(id, data) {
  const curso = Object.keys(data.matriculas).find(matricula => {
    return dataAtualizada.matriculas[matricula].id_curso === id;
  });
  return curso && curso.concluido;
}

If you want to change you can do so:

const json = {
  "user_cpf": "111.111.111-11",
  "user_name": "NOME",
  "matriculas": {
    "357847": {
      "mat_id": 357847,
      "id_curso": 2129,
      "concluido": true
    },
    "358696": {
      "mat_id": 358696,
      "id_curso": 4738,
      "concluido": true
    },
    "362953": {
      "mat_id": 362953,
      "id_curso": 5041,
      "concluido": false
    }
  }
};

function concluir(curso, data) {
  const dataAtualizada = JSON.parse(JSON.stringify(data));
  const concluido = Object.keys(dataAtualizada.matriculas).find(matricula => {
    return dataAtualizada.matriculas[matricula].id_curso === curso;
  });

  if (concluido) {
    dataAtualizada.matriculas[concluido].concluido = true;
  }
  return dataAtualizada;
}

const atualizado = concluir(5041, json);
console.log(atualizado);

The idea is to run those plates, and in case there’s one that has the id_curso sought to mark as complete. The function I created returns a new copy of the object, but you can remove this logic if you do not need an immutable logic.

  • Thank you very much, I will give a study now that you gave me a light. Only one thing, if it exists, has to check whether completed is equal to true?

  • 3

    @caiocabale everything is possible :) in which case the first lines of the code are enough const concluido = Object.keys(dataAtualizada.matriculas).find(matricula => { ...... }); and then do console.log(concluido. concluido);

  • I do not know if it was clear, the check of it can have double meaning is to check to change the value or to check to verify the value? @Sergio

  • 2

    To check, Also I got it, now I understand the logic!

  • @caiocafardo just show what’s inside concluido?

  • @caiocafardo joined an example of function verificarCursoConcluido, but I think you’ve already found the solution, too.

Show 1 more comment

3

A simple way, find the keys to this json (which is a dictionary of values) and after that an interaction to find or not the value that was asked and in the end present true or false contained in concluido, example:

var json = {"user_cpf":"111.111.111-11","user_name":"NOME","matriculas":{
    "357847":{"mat_id":357847,"id_curso":2129,"concluido":true},
    "358696":{"mat_id":358696,"id_curso":4738,"concluido":true},
    "362953":{"mat_id":362953,"id_curso":5041,"concluido":false}
}};

function busca_matricula_concluida(matriculas, id_curso)
{
    keys = Object.keys(matriculas);   
    i = 0;
    while(i < keys.length){
      if (matriculas[keys[i]].id_curso == id_curso){
        break;
      }
      i++;
    }
    return (i < keys.length) && (matriculas[keys[i]].concluido);
    
}

console.log(busca_matricula_concluida(json.matriculas, 4738));
console.log(busca_matricula_concluida(json.matriculas, 0));

Browser other questions tagged

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