Home > Blockchain >  how to change the format of json array by loping over
how to change the format of json array by loping over

Time:10-16

Hi I am getting data from API but I want my data in different format so that I can pass later into a function. I want to change the names of keys into a different one becasuse I have created a chart and it only draws if I send it data in certain way This is what I am getting from API

     data = {
                "status": "success",
                "from": "DB",
                "indice": "KSE100",
                "data": [
                    {
                        "stock_sector_name": "Tc",
                        "sector_score": "0828",
    
                        "stocks": [
                            {
                                "stock_symbol": "TRG",
                                "stock_score": 44
                            },
                            {
    
                                "stock_symbol": "SYS",
                                "stock_score": 33
                            }
                        ]
                    },
                    {
                        "stock_sector_name": "OIL",
                        "sector_score": "0828",
    
                        "stocks": [
                            {
                                "stock_symbol": "FFS",
                                "stock_score": 44
                            },
                            {
    
                                "stock_symbol": "SMS",
                                "stock_score": 33
                            }
                        ]
                    },
    
                ]
            }

But I want my data to look like this like this 
  

     data = {
                "name": "KSE100",
                "children": [
                    {
                        "name": "A",
                        'points': -9,
                        "children": [
                            {
                                "stock_title": "A",
                                "value": 12,
                            },
                            {
                                "stock_title": "B",
                                "value": 4,
                            },
                        ]
                    },
                    {
                        "name": "B",
                        'points': 20,
                        "children": [
                            {
                                "stock_title": "A",
                                "value": 12,
                            },
                            {
                                "name": "B",
                                "value": 4,
                                
                            },
    
                        ]
                    },
                ]
    
            }


   Like I want to replace 
    stock_sector_name = name
    sector_score = value
    stocks = children
    stock_symbol = name
    stock_score = value

I have been trying this for so much time but sill could not figured it out

CodePudding user response:

function convert(d){

    return {
      name : d.indice,
      children : d.data.map(y=>{
        return {
          name : y.stock_sector_name,
          points : y.sector_score,
          children : y.stocks.map(z=>{
            return {
              stock_title: z.stock_symbol,
              value : z.stock_score
            }
          })
        }
      })
    }

}

CodePudding user response:

You can do something like this

const data = {
  "status": "success",
  "from": "DB",
  "indice": "KSE100",
  "data": [{
      "stock_sector_name": "Tc",
      "sector_score": "0828",

      "stocks": [{
          "stock_symbol": "TRG",
          "stock_score": 44
        },
        {

          "stock_symbol": "SYS",
          "stock_score": 33
        }
      ]
    },
    {
      "stock_sector_name": "OIL",
      "sector_score": "0828",

      "stocks": [{
          "stock_symbol": "FFS",
          "stock_score": 44
        },
        {

          "stock_symbol": "SMS",
          "stock_score": 33
        }
      ]
    },

  ]
}




const data2 = {
  "name": "KSE100",
  "children": [{
      "name": "A",
      'points': -9,
      "children": [{
          "stock_title": "A",
          "value": 12,
        },
        {
          "stock_title": "B",
          "value": 4,
        },
      ]
    },
    {
      "name": "B",
      'points': 20,
      "children": [{
          "stock_title": "A",
          "value": 12,
        },
        {
          "name": "B",
          "value": 4,

        },

      ]
    },
  ]

}



//stock_sector_name = name
//sector_score = value
//stocks = children
//stock_symbol = stock_title
//stock_score = value

const keys = {
  stock_sector_name: "name",
  sector_score: "points",
  stocks: "children",
  stock_symbol: "stock_title",
  stock_score: "value",
  indice: "name",
  data: "children"
}

const rename = (value) => {
  if (!value || typeof value !== 'object') return value;
  if (Array.isArray(value)) return value.map(rename);
  return Object.fromEntries(Object
    .entries(value)
    .map(([k, v]) => [keys[k] || k, rename(v)])
  );
}
renamedObj = rename(data);

console.log(renamedObj);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related