Venktaish
Venktaish

Reputation: 141

JSON Parse to Object Weired Results?

I am parsing an json whose structure looks like this:

"records": [
        {
            "timestamp": "1604133302",
            "state": "Andhra Pradesh",
            "district": "Anantapur",
            "market": "Rayadurg",
            "commodity": "Maize",
        },
        {
            "timestamp": "1604133302",
            "state": "Andhra Pradesh",
            "district": "Chittor",
            "market": "Chittoor",
            "commodity": "Maize",
        },
.... 
]

and This is the code i tried to parse to object i want to show them in optional buttions in html where people selects the state and city and all :

jsonFile.records.forEach(function (s) {
    let { state, district, market, commodity } = s;
    // states[state] = district;
    // states[state][district] = market
    // states[state][district][market] = commodity;
    
    states[state] = {
        [district]: {
            [market]: [
                [commodity],
            ]
        }
    };
}); 

It's replacing the last updates.

How to properly parse.

The object structure i want is like this:

{
   "myState":{
      "myDistrict":{
         "myMarket":[
            "commoditynames"
         ],
         "myMarketAnother":[
            "commoditynames"
         ]
      },
      "myDistrictAnother":{
         "myMarket":[
            "commoditynames"
         ],
         "myMarketAnother":[
            "commoditynames"
         ]
      }
   }
} 

How can i parse this properly ?

Upvotes: 0

Views: 51

Answers (1)

Raghav Garg
Raghav Garg

Reputation: 3707

You just need to push the result not replace in the final commodity array.

We can use .reduce(), we can iterate over every element of an array and can maintain a final state of result. Read more about it Array.prototype.reduce().

On every iteration, we initialize our result with empty objects/array and then finally pushing the desired data.

function fixJSONStructure(records = {}) {
  
  return records.reduce((result, record) => {
      let { state, district, market, commodity } = record;
      
      // initialize with empty object
      if (!result[state]) result[state] = {};
      if (!result[state][district]) result[state][district] = {};
      if (!result[state][district][market]) result[state][district][market] = [];
      
      // pushing a commodity instead of replacing
      result[state][district][market].push(commodity);
      
      return result;
  }, {});
}

records = [
        {
            "timestamp": "1604133302",
            "state": "Andhra Pradesh",
            "district": "Anantapur",
            "market": "Rayadurg",
            "commodity": "Maize",
        },
        {
            "timestamp": "1604133302",
            "state": "Andhra Pradesh",
            "district": "Anantapur",
            "market": "Rayadurg",
            "commodity": "something else",
        },
        {
            "timestamp": "1604133302",
            "state": "Andhra Pradesh",
            "district": "Chittor",
            "market": "Chittoor",
            "commodity": "Maize",
        },
        {
            "timestamp": "1604133302",
            "state": "Karnatka",
            "district": "Bangalore",
            "market": "foobar",
            "commodity": "baz",
        },
];
console.log (fixJSONStructure(records));

Upvotes: 1

Related Questions