right now I'm having a nested JSON tree object (this.props.holdinginfo) sending from backend to frontend, and I am trying to add an EXPORT button on the webpage to be able to export the JSON object to a csv file using react.
The JSON tree object is as below:
[{'holder': 'Passive', 'Position': 500.0, 'pct_shares_out': 0.5,
'Node': [{'holder': 'TD Inc.', 'Position': 300.0, 'pct_shares_out': 0.2,
'Node': [{'holder': 'TD ETF', 'Position': 400, 'pct_shares_out': 0.1},
{'holder': 'TD Fund', 'Position': 430.0, 'pct_shares_out': 0.2}]},
{'holder': 'Active', 'Position': 725.0, 'pct_shares_out': 0.1,
'Node': [{'holder': '18 Asset', 'Position': 390, 'pct_shares_out': 0.5,
'Node': [{'holder': '18 Inc.', 'Position': 190, 'pct_shares_out': 0.2},
{'holder': 'Lysander-18 Equity Fund Series A', 'Position': 19, 'pct_shares_out': 0.05}]}]}];
And I want the csv file following the format below:
holder/position/pct_share_out
'Passive'/500.0/0.5
'TD Inc.'/300.0/0.2
'TD ETF'/400/0.1
'TD Fund'/430/0.2
'Active'/725/0.1
'18 Asset'/390/0.5
'18 Inc.'/190/0.2
'Lysander-18 Equity Fund Series A'/19/0.05
What I am doing right now is adding a button in js file in component:
<Button variant="contained"
size="small"
onClick={this.handleExport}>
<SaveIcon label="Export" />
Export
</Button>
which calls handleExport to export the table into a csv file:
handleExport = () => {
let csv = Papa.unparse(this.props.holdinginfo);
console.log(csv);
}
But since this.props.holdinginfo is a nested tree object, the output of console.log is ""[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object].... "
Question I have - 1- How to convert the nested tree JSON into csv? 2- How to download that csv when the user click the button?
Thanks for helping!!