Search JSON values for multiple keys

Asked

Viewed 660 times

1

I have the following JSON:

{
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
}

I need a function that looks for the key name and then looks for a number inside it, and returns the whole object. For example:

json("$MT", "91");

expected return:

"$MT": {
            "0": "91",
            "1": "00:00:34.187",
            "2": "00:18:44.001\n"
        }

I have the following function:

var json = {}; 
function addJson(data){
   var chave; 
   for(var i in data){
      if(i == 0){ 
         chave = data[i]; 
         json[chave] = {}; 
      }else{
         json[chave][i-1] = data[i]; 
      }
   }
}

But only one key returns to me.

  • You could better explain your problem by crafting a better example?

  • 1

    i receive this json every second, I need to filter for the first key that would be in the "$MT" example and then a second filter for the value "91" and the expectation is to resume the whole object so that I can use the other values of it

  • 1

    It would be good for you to give more examples because it’s hard to understand

3 answers

3

I did it this way, according to what I understood of the question, if it got something obscure comment here below that I help you.

let meuJSON = {
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
}

const getJSON = (estado, conteudo, json) => {
  const jsonEmArray = Object.values(json[estado]) // Transforma os valores em Array
  let resultado = {}

  jsonEmArray.forEach(elemento => {
    if (elemento === conteudo) resultado = json[estado]
  })

  return resultado
}

let jsonFiltrado = getJSON('$SP', '92', meuJSON)

console.log(jsonFiltrado)

0

@Eduardoribeiro works when I only call getJSON once, when I call for example

let jsonFiltrado = getJSON('$SP', '92', meuJSON)
let jsonFiltrado2 = getJSON('$MT', '91', meuJSON)

console.log(jsonFiltrado)
console.log(jsonFiltrado2)

does not return either of the two calls

let meuJSON = {
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
}

const getJSON = (estado, conteudo, json) => {
  const jsonEmArray = Object.values(json[estado]) // Transforma os valores em Array
  let resultado = {}

  jsonEmArray.forEach(elemento => {
    if (elemento === conteudo) resultado = json[estado]
  })

  return resultado
}

let jsonFiltrado = getJSON('$SP', '92', meuJSON)

console.log(jsonFiltrado)

0


You can simply access the attribute from the first key and then use the function Object.values() to check if the value is contained within the object

const buscar = (dados, chave, conteudo) => {
  const item = dados[chave];
  
  return Object.values(item).indexOf(conteudo) !== -1 ? item : null;
};

// Teste
const json = {
  "$SP": {
    "0": "92",
    "1": "00:01:36.340",
    "2": "00:05:48.929\n"
  },
  "$MT": {
    "0": "91",
    "1": "00:00:34.187",
    "2": "00:18:44.001\n"
  }
};

console.log(buscar(json, '$SP', '92'));
console.log(buscar(json, '$MT', '91'));

Object.values()

The Object.values() method returns an array with the values of the properties of a given object...

  • Now, I’m doing something wrong because I haven’t been able to get the return on multiple calls I get the json every second i.e., I get "$MT": { "0": "91", "1": "00:00:34.187", "2": "00:18:44.001 n" } in the next second "$SP": { "0": "92", "1": "00:01:36.340", "2": "00:05:48.929 n" }

Browser other questions tagged

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