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

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

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));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742389374a4434705.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信