Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

345
Visualizações
Get value from json of which key is not known (Javascript)

I have following JSON file:

{
  "status": 200,
  "msg": "OK",
  "result": {
    "files": {
      "count": 1,
      "pUnJbKzql0f2": {
        "name": "How do I access this",
        "active": "yes"
      }
    }
  }
}

I have to access the value of key name in above JSON file. The problem is that the key pUnJbKzql0f2 keeps changing every time the file is requested. I don't have control over the json file. So how do I access key name. Talking about PHP we can use array_keys or array_key_exists function, however I want javascript specific answer for it.

json['result']['files'][what should I put here]['name']

                      ^^^^^^^^^^^^^^^^^^^^^^^

Please if someone could post a solution here.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You have to examine the contents of .result.files. This can be done using a for-in loop where the keys are read.

This snippet illustrates finding the unknown key (and it's value) based on it not being the known key count. If the object has more properties you will have to devise a way of excluding those except the one you want.

let requiredKey = "";
let requiredName = "";
const obj = {
  "status": 200,
  "msg": "OK",
  "result": {
    "files": {
      "count": 1,
      "pUnJbKzql0f2": {
        "name": "How do I access this",
        "active": "yes"
      }
    }
  }
}

files = obj.result.files // and object with 2 properties

 for (key in files) {
 
 if (key != "count") {
 
 requiredKey = key;
 requiredName = files[key].name

 } // end if;
      
 } // next key in files

 console.log(`${requiredKey} with name property: ${requiredName}`)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda