Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

342
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!