skip to Main Content

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


  1. You can recursively sanitize the properties from the json:

    function sanitizeJson(obj) {
      // Object is an array? Then recursively sanitize each element
      if (Array.isArray(obj)) {
        return obj.map(sanitizeJson);
      }
      // Object is a non-null object
      else if (typeof obj === 'object' && obj !== null) {
        // Both '$id' and '$values' properties exist? Sanitize the '$values' property
        if (obj.hasOwnProperty('$id') && obj.hasOwnProperty('$values')) {
          return sanitizeJson(obj['$values']);
        }
        // Make a new object from the '$id' property and sanitize the remaining properties
        return Object.fromEntries(
          Object.entries(obj)
            .filter(([key]) => key !== '$id')
            .map(([key, value]) => [key, sanitizeJson(value)])
        );
      }
      return obj;
    }
    
    const json = '{"$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}]}}]}';
    const jsonObject = JSON.parse(json);
    const result = JSON.stringify(sanitizeJson(jsonObject), null, 't');
    
    console.log(result);
    Login or Signup to reply.
  2. The JSON.parse reviver and JSON.stringify replacer can be used to modify nested values :

    const obj = {"$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}]}}]};
    
    const json = JSON.stringify(obj, (k, v) => k == '$id' ? void 0 : v?.$values ?? v, 4);
    
    console.log( json );
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search