I have a string Json that comes from my backend that I’m trying to beatify it to show to the users, so I’m using the stringify to do this, but it keeps the "$id" and the "$value" to it, there is a way to remove it?
The code that I have is this:
let jsonFormated = JSON.stringify(JSON.parse(result.Value), null, 't');
the result.value came from the API that I’m calling, the value is a string that was serialized on the c# backend.
the result of it is this:
{
"$id": "1",
"$values": [
{
"$id": "2",
"prop1": "result",
"prop2": "result",
"prop3": {
"$id": "3",
"$values": [
{
"$id": "4",
"prop1": "result",
"prop2": null
},
{
"$id": "4",
"prop1": "result",
"prop2": null
},
]
}
}
]
}
I want to remove this properties "$id", "$values" and have only a structure equals that I recieved from the backend, but pretty printed, the structure that I recieve from the backend is that:
[{"prop1":"result","prop2":"result","prop3":[{"prop1":"result","prop2":"result"},{"prop1":"result","prop2":"result"}]}]
My problem is that I need it to be a string that I will send to a component, so I need the string to be correct
2
Answers
You can recursively sanitize the properties from the json:
The
JSON.parse
reviver andJSON.stringify
replacer can be used to modify nested values :