0

I am trying to remove some property of a JSON response.

I would like to remove this part of the response (first part) :

{
  "en" : 10,
  "left" : false,
  "right" : false,
  "result" : 

And remove too the last character of the response (last part) :

"}"

I have tried different ways to do this but none of them work as I would like them to.

var object_delete = ['en','left','right','result'];

for(i in object_delete){
  var data = JSON.parse(JSON.stringify(data).replace(object_delete[i], ""));
}

I expect the output :

[ {
  "type1" : "string data",
  "type2" : "string data",
  "type3" : {
    "name" : "string data"
  },
  "red" : {
    "path" : "string data",
    "path2" : null,
    "path3" : null,
    "path4" : null
  }
} ]

But the actual output is :

{

  "en" : 2,
  "left" : false,
  "right" : false,
  "result" : [ {

  "type1" : "string data",
  "type2" : "string data",
  "type3" : {
    "name" : "string data"
  },
  "red" : {
    "path" : "string data",
    "path2" : null,
    "path3" : null,
    "path4" : null
  }
} ]

}

Anyone have any idea how this can be done?

0xffxd
  • 3
  • 3
  • Disregarding the anti-pattern of replacing text in the JSON, then parsing it back into an object, all you want is `response.result`. (JSON is a way of sending an object as text; it should never be used for anything else) –  Jul 07 '19 at 19:02
  • you can simply use `response.result`. – Faraz Javed Jul 07 '19 at 19:05
  • Change it to a `for...of `loop and just delete each property? `for(let i of object_delete) delete data[i]` – adiga Jul 07 '19 at 19:05
  • @ChrisG Thanks for your reply. Do you have an example ? Because when I am trying to do that it is not the expected result. I get a undefined output.. Sorry I am new on javascript – 0xffxd Jul 07 '19 at 20:04
  • @FarazJaved Thanks for your help. As my answer above when I try to do that it doesn't work at all and I do not know why – 0xffxd Jul 07 '19 at 20:05
  • You were mentioning a "JSON response", but I guess your response object is called `data` which means you want `data.result` instead. You're asking the wrong question; what you want isn't to remove the stuff you don't need (let alone in string form), what you want is the array called `"result"` from the response you get. And since `"result"` is simply a prop/child of the main object, all you need to do is to put `.response` or `["response"]` behind the object itself. See also this question: https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets –  Jul 07 '19 at 20:39
  • @ChrisG Thanks again for your help! It works I get the expected result now :) I just used properly the "response.result" as you said above – 0xffxd Jul 07 '19 at 20:51
  • Right, sorry, I meant `.result` or `["result"]`. But you figured it out anyway :) –  Jul 07 '19 at 20:55
  • @ChrisG Thanks for your clarifications I appreciated – 0xffxd Jul 07 '19 at 21:04

1 Answers1

0

You could try something like this:

const entry = {
  "entries" : 10,
  "left" : false,
  "right" : false,
  "result" : [
    {
      "type1" : "string data",
      "type2" : "string data",
      "type3" : "string data",
      "type4" : "string data",
      "type5" : "string data",
      "type6" : {
        "name" : "string data"
      },
      "red" : {
        "path" : "string data",
        "path2" : null,
        "path3" : null,
        "path4" : null
      }
    }
 ]
};

const output = [...entry["result"]];

console.log(JSON.stringify(output));

// [{"type1":"string data","type2":"string data","type3":"string data","type4":"string data","type5":"string data","type6":{"name":"string data"},"red":{"path":"string data","path2":null,"path3":null,"path4":null}}]
Ion
  • 1,262
  • 12
  • 19