1

I am using "react-json-to-csv" in order to save my json data into a csv file, the problem is with the nested objects because the csv export header is broken, displaying alternate data due to those nested objects. For Example this is my json data:

[
    {
        "id": "997",
        "source": "wired",
        "title": "testing title",
        "www": {
            "wwwId": 918,
            "wwwName": "Deprecated wire)"
        },
        "css": 7.3,
        "wrongAdded": "true",
        "paths": [
            {
                "name": "client",
                "version": "1.9.2",
                "description": "testing",
                "resolvePath": {
                    "name": "ares",
                    "isFsfLibre": true,
                    "isold": false
                },
                "externalPath": [
                    {
                        "type": "a",
                        "url": "was"
                    },
                    {
                        "type": "q",
                        "url": "wss"
                    },
                    {
                        "type": "x",
                        "url": "was"
                    }
                ],
                "project": {
                    "name": "Testing",
                    "version": "001",
                    "active": true
                },
                "usedBy": 0,
                "isInternal": false
            }
        ],
        "Count": 0
    }, ... and so on
]

and in this way I am getting my api data:

 const [stateData, setData] = useState({infoData:[]});
.......
    function callingApis() {
    
        const promises = urls.map(url => axios.get(url, { headers }));
    
        Promise.all(promises).then(responses => {
            let data = [];
    
            responses.forEach(response => {
                data = data.concat(response.data);
            });
       setData(data);
        });
    };

Maybe someone have a better Ideea about how to do that?

Or it is possible to store in a variable some pieces from the json object like:

const filteredData = [
{"id": stateData.id},
{"source": stateData.source},
{"pathName": stateData.paths.name}
{"pathDescription": stateData.paths.description},
and so on ...
]

But I guess there is a need to do .map() over the data before filtering ?

and at the end to export it to "react-json-to-csv".

<CsvDownload data={filteredData} />

1 Answer 1

2

I'm not sure this is the answer you are looking to but it seems that you just have to use map to create an array of object with the shape you are looking for.

function callingApis() {

    const promises = urls.map(url => axios.get(url, { headers }));

    Promise.all(promises).then(responses => 
        responses.map(({data}) => {
           return [
             {"id": data.id},
             {"source": data.source},
             {"pathName": data.paths.name}
             {"pathDescription": data.paths.description},
          ]
     ).then(filteredData => setData(filteredData))
   
   
};
Sign up to request clarification or add additional context in comments.

3 Comments

seems to be a good ideea but I still need the concat because there are multiple api
@Jimmy'sCheese this will work with multiple api call and you don't need to concat because Promise.all return you an array with all the data from your promises
but seems that is crashing : responses.map(...).then is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.