title: NodeJS Lambda function: how do I parse and detect a key inside an event
body: <p>I am using <a href="https://www.npmjs.com/package/lambda-local" rel="nofollow noreferrer">Lambda-local</a> to test my event, which will simulate a JSON file being sent to my S3 bucket and ultimately that i need to save. </p>
  
  <p>Here is my event.json:</p>
  
  <pre><code>{
      "Records":[
        {
          "s3":{
            "bucket":{
            "name": "my.bucket"
        },
        "object":{
          "key": "report.json",
          "body": "{myKey: value}"
          }
       }
      }
     ]
    }
  </code></pre>
  
  <p>I would like to be able to run an <code>s3.putObject</code> if and only if the event contains a specific key in the body (in my little example, <code>mykey</code>). </p>
  
  <p>I have attempted at the following:</p>
  
  <pre><code>exports.handler = (event) =&gt; {
  
      console.log('Received event:', JSON.stringify(event, null, 2));
      let eventKey = event.body.some((val) =&gt; {
          return Object.keys(val).includes("myKey");
      });
  }
  </code></pre>
  
  <p>This fails like other attempts and it is because <code>body</code> is actually undefined. Is it possible to read a key from an incoming JSON event? Ultimately where the JSON is saved (inside the bucket) is dictated by the key inside the JSON file. This is fine if I work on a concrete file i either upload or send via SDK, but not if I use the event parameter. Can you please tell me whether this is the correct way to test my function to be production ready (meaning to be ready to be triggered by incoming events). Can you please help? </p>
  
  <p>Thank you</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Amongst various attempts at also try this, but with no joy:</p>
  
  <pre><code>exports.handler = (event) =&gt; {
  
      console.log('Received event:', JSON.stringify(event, null, 2));
  
      const key = 'mykey';
      const obj = JSON.stringify(event, null, 2).body; 
  
      if(key in obj){
          console.log('yep')
      }
      else{
          console.log('no party');
      }
  }
  </code></pre>
  
  <p>This time I am returned with the error:</p>
  
  <pre><code>{"errorType":"TypeError","errorMessage":"Cannot use 'in' operator to search for 'myKey' in undefined","stack":["TypeError: Cannot use 'in' operator to search for 'myKey' in undefined","    at Runtime.exports.handler (/var/task/index.js:8:12)","    at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)","    at process._tickCallback (internal/process/next_tick.js:68:7)"]}
  </code></pre>
  
  <p>Any idea?</p>