In React I'm trying to render the JSON data I'm getting from the server to readable data.
Here is the string from the server
"[{\"mat_type\":1,\"mat_title\":\"Title1\",\"mat_unit\":2,\"mat_price\":44000,\"mat_cost\":2000},{\"mat_type\":2,\"mat_title\":\"Title2\",\"mat_unit\":2,\"mat_price\":44000,\"mat_cost\":2000},{\"mat_id\":1,\"mat_title\":\"Title\",\"mat_unit\":2,\"mat_price\":4400,\"mat_cost\":2000,\"mat_status\":0}]"
I want to make it readable data with newline and spacing on it.
Here is my quick function
<Typography>
{{JSON.parse(item.api_text).map((item) => {
console.log(item);
return `<br>${JSON.stringify(item, null, '\t')}</br>`;
})} }
</Typography>
Here item.api_text is the string above. I know this will create error but I want something like this. Thanks
Edit: I want to display it like this on document not in console.
[{
"mat_type": 1,
"mat_title": "Title1",
"mat_unit": 2,
"mat_price": 44000,
"mat_cost": 2000
},
{
"mat_type": 2,
"mat_title": "Title2",
"mat_unit": 2,
.....
'\t'might be in the wrong place. Try usingJSON.stringify(item, '\t', 2)<pre>element