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

184
Views
How to make a Javascript objects from given JSON data format

Hi I need help understanding how can I create objects from given JSON data:

   {
   "type":"FeatureCollection",
   "features":[

      {
         "type":"Feature",
         "properties":{
            "dbh":6
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               -79.96474,
               40.46283
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "dbh":2
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               -80.00949,
               40.42532
            ]
         }
      },
      {
         "type":"Feature",
         "properties":{
            "dbh":12
         },
         "geometry":{
            "type":"Point",
            "coordinates":[
               -79.93531,
               40.42282
            ]
         }
      }

I have done following:

let rsuCountsData = await axios.get('https://rsu-manager-apigateway 5xm3e3o5.uc.gateway.dev/rsucounts');
      this.createGeoJson(rsuCountsData.data); //calling the below function

I created the following function to get the value of count :

createGeoJson(data){
 //geojson={}
 //extract the count from data and store it in a object
 for (let [key, value] of Object.entries(data)) {
  console.log(key, value.count); //key= ip address

}

}

Now inside this function I want to create objects from that above JSON data but I am not sure how to do that because it looks too nested for me figure it out.Below is what I have done.It is mix of pseudo code and JS.Also don't think its correct but can someone help me point in correct direction how I can store these values in JS objects inside createGeoJson function:

     geojson = {}
      geojson.type = "FeatureCollection"
      geojson.features = []

   for key, val in data:
   feat.type = "Feature"
   props.count = val.count
    props.ipAddress = val.ipAddress// this is the key in above function.
   feat.properties = props
   geojson.features.append(feat)

Thanks and appreciate any input

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

0

I would suggest you to use JSON.parse() to convert JSON string to plain JS object.

After that you can iterate it with for...in, loops or any other iterator.

about 4 years ago · Juan Pablo Isaza Report

0

Generally, (after parsing your string via JSON.parse()) you can loop over your Object.feature array with the .forEach function (see MDN documentation).

Not 100% sure what your exact output should be but this looks like a good use case for the .map() function (MDN documentation here). Say, you have your JSON stored in const json, you could do something like:

const json = { ... }
const newArray = json.features.map(el => Object.assign({},{type: el.type, properties: el.properties, geoType: el.geometry.type}));

newArray will be filled with Objects created from empty Objects and the Object.assign() function (MDN documentation)

EDIT: You can also access the index within the .map() function for your counter.

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!