PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, July 25, 2022

[FIXED] how can i retrieve value in JSON string in lambda python3

 July 25, 2022     aws-event-bridge, aws-lambda, json, python     No comments   

Issue

I'm developing an AWS lambda function that is triggered from an event bridge and then putting another event using python but struggling to retrieve a value from a variable in the Json string

below is the code

import json, boto3

client = boto3.client('events')

def lambda_handler(event, context):

testV2_dict={  
 "K1" : event['e1'] ,
"K2" : event['e2'] 
}

#converting python to json as (put_event - Details) section is expecting json
testV2=json.dumps(testV2_dict)

response = client.put_events(
         Entries=
          [
            {
             "DetailType": "test",
             "Source": "test",
             "Detail": "{ \"testK\": \"testV\",\"testK2\": \""+ testV2 +"\" }"
           }
          ]
        )

tried to add Details on different ways, "Detail": "{ \"testK\": \"testV\",\"testK2\": \""+ testV2 +"\" }" and still getting error as Malformated Details and if i deleted the ++, I'm getting word testV2 itself not the value from the above

How can I retrieve the value of testV2 in the Details inside the event?


Solution

You don't have enough escaping in there. If testV2 is supposed to be a JSON string emebedded in a JSON string embedded in as JSON string, then you need more string escapes. I would let json.dumps handle that:

import json
event = {'e1': 99, 'e2': 101}

testV2_dict={  
 "K1" : event['e1'] ,
 "K2" : event['e2'] 
}

testV2=json.dumps(testV2_dict)

detail = {
    "testK": "testV",
    "testK2": testV2
}

Entries= [
    {
     "DetailType": "test",
     "Source": "test",
     "Detail": json.dumps(detail),
   }
]

print(Entries)

Output:

[{'DetailType': 'test', 'Source': 'test', 'Detail': '{"testK": "testV", "testK2": "{\\"K1\\": 99, \\"K2\\": 101}"}'}]


Answered By - Tim Roberts
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing