-4

I have a JSON (All under req.body) in the format

{body :
   { Cust : {...} },
   { Input : {...} },
   { Reciept : {image: [Array] } } 
}

I want to be able to remove all the key Reciept so the new JSON would look like ...

{body :
   { Cust : {...} },
   { Input : {...} }
}

I've tried to use delete req.body.Reciept and delete req.body.Reciept.image Both are unable to change the JSON for me. What should I be doing?

edit: When I console log req.body this is what I get :

{ body:
   { CustData:
      { customer: 'T',
        address1: '',
        address2: '',
        billing: '',
        signature: [Object] },
     InputData:
      { InputDate: '2019-10-21 23:25:28',
        Workers: [],
        Equipment: [],
        Purchases: [] },
     Reciept: { image: [Array] } } }

I haven't found a solution because I have a JSON and then an array as a value for a key. I am simply trying to remove the whole Reciept key and everything attached to it.

Here is what is I am running

console.log(req.body);
delete req.body.Receipt;
console.log(req.body);

Here is what I get returned in the terminal

{ body:
   { CustData:
      { customer: 'Test',
        address1: '',
        address2: '',
        billing: '',
        signature: [Object] },
     InputData:
      { InputDate: '2019-10-22 0:9:33',
        Workers: [],
        Equipment: [],
        Purchases: [] },
     Receipt: { image: [Array] } } }

//followed by
{ body:
   { CustData:
      { customer: 'Test',
        address1: '',
        address2: '',
        billing: '',
        signature: [Object] },
     InputData:
      { InputDate: '2019-10-22 0:9:33',
        Workers: [],
        Equipment: [],
        Purchases: [] },
     Receipt: { image: [Array] } } }
NSS
  • 3
  • 3
  • 1
    The syntax of your input is invalid. – CertainPerformance Oct 22 '19 at 06:59
  • First provided structure is incorrect. Second, please add attempt – Rajesh Oct 22 '19 at 06:59
  • 2
    *"I have a JSON"* Probably not. JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. It sounds like you have an *object*. – T.J. Crowder Oct 22 '19 at 07:00
  • Once you know that JSON is the wrong term, [the search](/search?q=%5Bjs%5D+remove+property+from+object) is fairly straightforward. Happy coding! – T.J. Crowder Oct 22 '19 at 07:01
  • **I suspect this problem is just a typo.** *"I've tried to use delete req.body.Reciept"* That's how you do it. But note that it's "Receipt," not "Reciept." Your question shows "Reciept" in the object too, but that's clearly something you've typed for the question. I suspect it's actually "Receipt" in the object but your code has "Reciept" and that's why it's not working. For us to help you, we need a [mcve] of the problem. – T.J. Crowder Oct 22 '19 at 07:03
  • I copied over the code this time, so the spelling is exactly what is running. – NSS Oct 22 '19 at 07:11

1 Answers1

0

JSON-> javascript object notation. It is just an object with key and value .Ntg else. Treat is as object nothing more specific than that. you have clearly one object which holds key as body and data is object which have nested key and value pair. Since the JSON format is text only, it can easily be sent to and from a server, and used as a data format by any programming language.

JavaScript has a built in function to convert a string, written in JSON format, into native JavaScript objects:

JSON.parse()

So, if you receive data from a server, in JSON format, you can use it like any other JavaScript object.Hope this will clear you concept of json.

delete request.body.Reciept; It is working.

enter image description here

Ayushi Keshri
  • 680
  • 7
  • 18
  • Is there anyway to remove the Purchases key all together? – NSS Oct 22 '19 at 07:22
  • you just have to dig down the key and its node you can use delete request.body.InputData.Purchases; – Ayushi Keshri Oct 22 '19 at 07:24
  • Could you reference me to what I would search to do that, or give me an example? Thank you for the detailed response though. – NSS Oct 22 '19 at 07:25
  • https://www.w3schools.com/howto/howto_js_remove_property_object.asp visit this site and clear your concept .It will be quite helpful to you in future :) can go to all the javascript concept from this site..Explore and learn.Happy coding! – Ayushi Keshri Oct 22 '19 at 07:25
  • Yes, how do I accept ? Sorry this is my first question – NSS Oct 22 '19 at 07:29