SaiRupa Golla
SaiRupa Golla

Reputation: 13

Parsing nested json data to json in reactjs

I need to parse nested JSON data to normal JSON which is coming from an API and need to pass that JSON data to a react table (am using the react-table library) like below :

Nested Json data:

      {
       "value":[
          {
             "id":"d38070fd",
             "name":"webwhatsapp",
             "url":"webwhatsapp.com",
             "project":{
                "id":"89ea5860-8dce",
                "name":"whatsapp",
                "url":"",
                "state":"well",
                "revision":10612,
                "visibility":"private",
                "lastUpdateTime":"2021-01-27T11:30:55.523Z"
             },
             "defaultBranch":"branchname",
             "size":33758407,
             "remoteUrl":"remote",
             "sshUrl":"ssh",
             "webUrl":"weburl",
             "isDisabled":false
          },
          {
             "id":"b0dd02f7",
             "name":"git",
             "url":"github.com",
             "project":{
                "id":"89ea5860",
                "name":"core",
                "url":"github.com",
                "state":"well",
                "revision":10612,
                "visibility":"private",
                "lastUpdateTime":"2021-01-27T11:30:55.523Z"
             },
             "defaultBranch":"branchname",
             "size":30654059,
             "remoteUrl":"url",
             "sshUrl":"url.git",
             "webUrl":"url.git",
             "isDisabled":false
          },
            ]
             },
             count: 90
            }

To this JSON:

{
   "value":[
      {
         "id":"d38070fd",
         "name":"webwhatsapp",
          "url":"webwhatsapp.com",
         "defaultBranch":"branchname",
         "size":33758407,
         "remoteUrl":"remote",
         "sshUrl":"ssh",
         "webUrl":"weburl",
         "isDisabled":false
         "project.id":"89ea5860-8dce",
          "project.name":"whatsapp",
          "project.url":"",
           "project.state":"well",
           "project.revision":10612,
           "project.visibility":"private",
           "project.lastUpdateTime":"2021-01-27T11:30:55.523Z"
         
        
          
      },
      {
         "id":"b0dd02f7",
         "name":"git",
         "url":"github.com",
          "defaultBranch":"branchname",
         "size":30654059,
         "remoteUrl":"url",
         "sshUrl":"url.git",
         "webUrl":"url.git",
         "isDisabled":false
            "project.id":"89ea5860",
            "project.name":"core",
            "project.url":"github.com",
            "project.state":"well",
            "project.revision":10612,
            "project.visibility":"private",
            "project.lastUpdateTime":"2021-01-27T11:30:55.523Z"
         
        
      },

Below is the code:

import React, { useState, useEffect } from "react";
import { Row, Col } from "react-bootstrap";
import axios from "axios";

const App = () => {
   const[data,setData] = useState()
    let api = "apiurl";
    let token = "token";
        
            useEffect(() => {
                axios.get(api, { headers: {"Authorization" : `Basic ${token}`} })
            .then(res => {
              
                console.log(res)
                setData(res)
            })
            .catch(err =>{
                
                console.log('error',err)
            })
                
            },[]);
            
            }
     
export default App;

I can see the data in the console but am unable to parse JSON like above. Can someone please help with this? Thanks in advance

Upvotes: 0

Views: 241

Answers (1)

Nayan shah
Nayan shah

Reputation: 551

What I understand is, you need only the value part of JSON(API result).

so instead of doing

setData(res);

you should do

setData(JSON.parse(res).value)

Upvotes: 1

Related Questions