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

198
Views
Turning JSON file content into Javascript object

What I want to do is read the JSON from a file and turn it into an array in Javascript.

My JSON looks like this

{
  "3-2": "10",
  "3-3": "20",
  "3-4": "30",
  "5-1": "4",
  "5-3": "8"
}

I know I should include the code I'm working with, but I've tried so many different ways and I cannot get anything to even remotely work, so I'm just looking for a simple solution (I'm using jQuery).

I just want to fetch the content of my JSON file, turn it into an array, and then be able to run a for loop to alert each item in the array.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

const json = '{"3-2":"10","3-3":"20","3-4":"30","5-1":"4","5-3":"8"}'
const obj = JSON.parse(json)
const arr = Object.entries(obj)

for (let i = 0; i < arr.length; i++) {
  const [key, val] = arr[i]
  console.log(`key: ${key}`)
  console.log(`val: ${val}`)
}

about 4 years ago · Juan Pablo Isaza Report

0

You can do it this way:

const yourJSON = {
  "3-2": "10",
  "3-3": "20",
  "3-4": "30",
  "5-1": "4",
  "5-3": "8"
}

for (key in yourJSON)
  console.log(key,":",yourJSON[key]) // here's your loop without converting into array

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!