javascript - How to remove nested JSON.stringify() properties - Stack Overflow

admin2025-03-19  2

I'm trying to modify a string with Typescript. The string is created by the JSON.stringify() method.

I want to remove the properties "id", "lightStatus" and the "value" attributes of "inputPort" and "outputPort". (I only need their attribute "id".)

console.log(JSON.stringify(this.light));
// Results in -> {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}

I tried to do it the following way but it doesn't recognize "inputPort.id" and "outputPort.id". This is what I tried and what it resulted in.

var savedLight = JSON.stringify(this.light, ["name", "inputPort.id", "outputPort.id", "resistance"]);
// Results in -> {"name":"Light Switch","resistance":100}

The result should include the properties "name", "inputPort id", "outputPort id" and "resistance". Like this:

{"name":"Light Switch","inputPort": 2, "outputPort": 2, "resistance":100}

Can anyone help me how I can get rid of the unnecessary properties?

I'm trying to modify a string with Typescript. The string is created by the JSON.stringify() method.

I want to remove the properties "id", "lightStatus" and the "value" attributes of "inputPort" and "outputPort". (I only need their attribute "id".)

console.log(JSON.stringify(this.light));
// Results in -> {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}

I tried to do it the following way but it doesn't recognize "inputPort.id" and "outputPort.id". This is what I tried and what it resulted in.

var savedLight = JSON.stringify(this.light, ["name", "inputPort.id", "outputPort.id", "resistance"]);
// Results in -> {"name":"Light Switch","resistance":100}

The result should include the properties "name", "inputPort id", "outputPort id" and "resistance". Like this:

{"name":"Light Switch","inputPort": 2, "outputPort": 2, "resistance":100}

Can anyone help me how I can get rid of the unnecessary properties?

Share Improve this question edited Apr 1, 2017 at 14:38 eko 40.7k11 gold badges78 silver badges101 bronze badges asked Apr 1, 2017 at 14:29 BRsmoverBRsmover 8872 gold badges11 silver badges21 bronze badges 2
  • what is the expected json data and what you get from the response. update it clearly. – Aravind Commented Apr 1, 2017 at 14:32
  • @Aravind I added the expected output. The result I got so far doesn't include the "inputPort" and the "outputPort" which is what I would want. – BRsmover Commented Apr 1, 2017 at 14:37
Add a ment  | 

3 Answers 3

Reset to default 5

You can pass a "replacer" function that returns the exact value you want.

var data = {"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100};

var result = JSON.stringify(data, function(k, v) {
    switch (k) {
    case "": case "name": case "resistance":
    	return v
    case "inputPort": case "outputPort":
    	return v.id
    default:
    	return undefined;
  }
}, 2)

document.querySelector("pre").textContent = result
<pre></pre>

The "" represents the top level object. For that, "name", and "resistance", it simply returns the original value.

For "inputPort" and "outputPort" it returns the id property.

Anything else gets undefined, which means it gets omitted from the result.

You can use a replacer function for this.

var obj = {
  "id": 1,
  "name": "Light Switch",
  "lightStatus": true,
  "inputPort": {
    "id": 2,
    "value": 0
  },
  "outputPort": {
    "id": 2,
    "value": false
  },
  "resistance": 100
};

var stringified = JSON.stringify(obj, function(key, val) {
  if (key === 'id' || key === 'lightStatus') {
    return void(0);
  }
  if (key === 'inputPort' || key === 'outputPort') {
    return val.id;
  }
  return val;
});

console.log(stringified);

You can apply Replacer function of JSON.stringify

var data='{"id":1,"name":"Light Switch","lightStatus":true,"inputPort":{"id":2,"value":0},"outputPort":{"id":2,"value":false},"resistance":100}';
var json=JSON.parse(data);

function replacer(i, val) {
  switch (i) {
    case "": case "name": case "resistance":
        return val
    case "inputPort": case "outputPort":
        return val.id
    default:
        return undefined;
  }

}

console.log(JSON.stringify(json,replacer));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1742389375a211165.html

最新回复(0)