remove field from response output in python











up vote
1
down vote

favorite












Can any suggest how to remove field EventTime from below output in python3 using boto3 .



'Events': [{
'EventId': '4a9f4c8e-3394-41df-xxxx-7e725b7c728e',
'EventName': 'ConsoleLogin',
'ReadOnly': 'false',
'EventTime': datetime.datetime(2018, 11, 5, 9, 49, 19, tzinfo = tzlocal()),
'EventSource': 'signin.amazonaws.com',
'Username': 'tests.sde',
'Resources': ,
'CloudTrailEvent': '{"eventVersion":"1.05","userIdentity":









share|improve this question






















  • Why do you want to remove it? The nice thing about json and/or dictionaries is that you can just ignore the keys/values you don't care about. You rarely need to actively remove them.
    – Matt Messersmith
    Nov 5 at 20:10

















up vote
1
down vote

favorite












Can any suggest how to remove field EventTime from below output in python3 using boto3 .



'Events': [{
'EventId': '4a9f4c8e-3394-41df-xxxx-7e725b7c728e',
'EventName': 'ConsoleLogin',
'ReadOnly': 'false',
'EventTime': datetime.datetime(2018, 11, 5, 9, 49, 19, tzinfo = tzlocal()),
'EventSource': 'signin.amazonaws.com',
'Username': 'tests.sde',
'Resources': ,
'CloudTrailEvent': '{"eventVersion":"1.05","userIdentity":









share|improve this question






















  • Why do you want to remove it? The nice thing about json and/or dictionaries is that you can just ignore the keys/values you don't care about. You rarely need to actively remove them.
    – Matt Messersmith
    Nov 5 at 20:10















up vote
1
down vote

favorite









up vote
1
down vote

favorite











Can any suggest how to remove field EventTime from below output in python3 using boto3 .



'Events': [{
'EventId': '4a9f4c8e-3394-41df-xxxx-7e725b7c728e',
'EventName': 'ConsoleLogin',
'ReadOnly': 'false',
'EventTime': datetime.datetime(2018, 11, 5, 9, 49, 19, tzinfo = tzlocal()),
'EventSource': 'signin.amazonaws.com',
'Username': 'tests.sde',
'Resources': ,
'CloudTrailEvent': '{"eventVersion":"1.05","userIdentity":









share|improve this question













Can any suggest how to remove field EventTime from below output in python3 using boto3 .



'Events': [{
'EventId': '4a9f4c8e-3394-41df-xxxx-7e725b7c728e',
'EventName': 'ConsoleLogin',
'ReadOnly': 'false',
'EventTime': datetime.datetime(2018, 11, 5, 9, 49, 19, tzinfo = tzlocal()),
'EventSource': 'signin.amazonaws.com',
'Username': 'tests.sde',
'Resources': ,
'CloudTrailEvent': '{"eventVersion":"1.05","userIdentity":






python-3.x boto3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 5 at 17:31









ravindert

61




61












  • Why do you want to remove it? The nice thing about json and/or dictionaries is that you can just ignore the keys/values you don't care about. You rarely need to actively remove them.
    – Matt Messersmith
    Nov 5 at 20:10




















  • Why do you want to remove it? The nice thing about json and/or dictionaries is that you can just ignore the keys/values you don't care about. You rarely need to actively remove them.
    – Matt Messersmith
    Nov 5 at 20:10


















Why do you want to remove it? The nice thing about json and/or dictionaries is that you can just ignore the keys/values you don't care about. You rarely need to actively remove them.
– Matt Messersmith
Nov 5 at 20:10






Why do you want to remove it? The nice thing about json and/or dictionaries is that you can just ignore the keys/values you don't care about. You rarely need to actively remove them.
– Matt Messersmith
Nov 5 at 20:10














2 Answers
2






active

oldest

votes

















up vote
0
down vote













The response syntax above is just a standard python dictionary. The one complication with the boto3 response syntax is the Events key is really a dictionary wrapped in a list, but you can still use pop() to remove the EventTime key from that dictionary. In the example below I've named the output response.



response['Events'][0].pop('EventTime', None)
print(response)


pop() removes keys in-place so after you run the code and look at response the EventTime key will be removed.






share|improve this answer




























    up vote
    0
    down vote













    This is a simple python element deletion from a dictionary. In your case you have a list of dictionaries. If you want to delete EventTime in all the dictionaries that are in the 'Events':[{}]. you can use the below code:



    for event in response['Events']:
    try:
    del event['EventTime']
    except KeyError:
    print("Key 'EventTime' is not found")


    The above code will delete the 'EventTime' and throws an error if it doesn't find any element with key EventTime. In your case, I guess all the dictionaries will have it.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159356%2fremove-field-from-response-output-in-python%23new-answer', 'question_page');
      }
      );

      Post as a guest
































      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      The response syntax above is just a standard python dictionary. The one complication with the boto3 response syntax is the Events key is really a dictionary wrapped in a list, but you can still use pop() to remove the EventTime key from that dictionary. In the example below I've named the output response.



      response['Events'][0].pop('EventTime', None)
      print(response)


      pop() removes keys in-place so after you run the code and look at response the EventTime key will be removed.






      share|improve this answer

























        up vote
        0
        down vote













        The response syntax above is just a standard python dictionary. The one complication with the boto3 response syntax is the Events key is really a dictionary wrapped in a list, but you can still use pop() to remove the EventTime key from that dictionary. In the example below I've named the output response.



        response['Events'][0].pop('EventTime', None)
        print(response)


        pop() removes keys in-place so after you run the code and look at response the EventTime key will be removed.






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          The response syntax above is just a standard python dictionary. The one complication with the boto3 response syntax is the Events key is really a dictionary wrapped in a list, but you can still use pop() to remove the EventTime key from that dictionary. In the example below I've named the output response.



          response['Events'][0].pop('EventTime', None)
          print(response)


          pop() removes keys in-place so after you run the code and look at response the EventTime key will be removed.






          share|improve this answer












          The response syntax above is just a standard python dictionary. The one complication with the boto3 response syntax is the Events key is really a dictionary wrapped in a list, but you can still use pop() to remove the EventTime key from that dictionary. In the example below I've named the output response.



          response['Events'][0].pop('EventTime', None)
          print(response)


          pop() removes keys in-place so after you run the code and look at response the EventTime key will be removed.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 5 at 20:09









          vealkind

          1,156810




          1,156810
























              up vote
              0
              down vote













              This is a simple python element deletion from a dictionary. In your case you have a list of dictionaries. If you want to delete EventTime in all the dictionaries that are in the 'Events':[{}]. you can use the below code:



              for event in response['Events']:
              try:
              del event['EventTime']
              except KeyError:
              print("Key 'EventTime' is not found")


              The above code will delete the 'EventTime' and throws an error if it doesn't find any element with key EventTime. In your case, I guess all the dictionaries will have it.






              share|improve this answer

























                up vote
                0
                down vote













                This is a simple python element deletion from a dictionary. In your case you have a list of dictionaries. If you want to delete EventTime in all the dictionaries that are in the 'Events':[{}]. you can use the below code:



                for event in response['Events']:
                try:
                del event['EventTime']
                except KeyError:
                print("Key 'EventTime' is not found")


                The above code will delete the 'EventTime' and throws an error if it doesn't find any element with key EventTime. In your case, I guess all the dictionaries will have it.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  This is a simple python element deletion from a dictionary. In your case you have a list of dictionaries. If you want to delete EventTime in all the dictionaries that are in the 'Events':[{}]. you can use the below code:



                  for event in response['Events']:
                  try:
                  del event['EventTime']
                  except KeyError:
                  print("Key 'EventTime' is not found")


                  The above code will delete the 'EventTime' and throws an error if it doesn't find any element with key EventTime. In your case, I guess all the dictionaries will have it.






                  share|improve this answer












                  This is a simple python element deletion from a dictionary. In your case you have a list of dictionaries. If you want to delete EventTime in all the dictionaries that are in the 'Events':[{}]. you can use the below code:



                  for event in response['Events']:
                  try:
                  del event['EventTime']
                  except KeyError:
                  print("Key 'EventTime' is not found")


                  The above code will delete the 'EventTime' and throws an error if it doesn't find any element with key EventTime. In your case, I guess all the dictionaries will have it.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 19:12









                  user2534033

                  116




                  116






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53159356%2fremove-field-from-response-output-in-python%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest




















































































                      這個網誌中的熱門文章

                      Xamarin.form Move up view when keyboard appear

                      Post-Redirect-Get with Spring WebFlux and Thymeleaf

                      Anylogic : not able to use stopDelay()