0

If i have a python json and i want to assigned a variable to one of the values in the python json,

if booking.hotel.feedback_option == True:
                name = request.GET.get('firstname')
                email= request.GET.get('email')
                values = """
                  {
                    "Name" ="'+str(name)+'",
                    "HasExternalDoubleOptIn": true,
                    "Email"="'+str(email)+'",
                    "CustomFields": [
                    "Checkout=2018-09-07"
                    ]
                  }
                """
                headers = {
                  'Content-Type': 'application/json',
                  'Accept': 'application/json'
                }
                data = values.encode()
                req = Request('https://api.moosend.com/v3/subscribers/7c343r9ad-er56-4c51-810e-74e27f8505c2/subscribe.json?apikey=77f343914-4t3c-4d61-8435-9346f5b4adf6', data=data, headers=headers)
                response_body  = urlopen(req).read()
5
  • How is that string being created? It's not standard dictionary syntax and it's also not valid JSON Commented Sep 18, 2018 at 6:59
  • @roganjosh please can you view my updated code i just pasted when i try to add variable to Name it give me error, if i just put in a name like "james" it work Commented Sep 18, 2018 at 7:01
  • The updated code hasn't changed the string, so my comment remains the same. It looks like you're hard-coding the string yourself? You do not have a dictionary. Commented Sep 18, 2018 at 7:02
  • You also don't have JSON, as I said in my first comment. As I asked originally, where is values coming from? Or is it something you intend to send? Commented Sep 18, 2018 at 7:11
  • the values are to be send to my mailing list as new subcribers. without adding the variable to Name and email and just put in a sting like "Name":"Jim" and " Email":[email protected]" it works it send the values across perfectly and create a new subscriber on my mailing list Commented Sep 18, 2018 at 7:14

2 Answers 2

2

IIUC, the better way to do this would be to create a python dictionary and then convert to JSON:

import json

name = 'Something'
email = 'Something else'

values = {
          "Name": name,
          "HasExternalDoubleOptIn": True,
          "Email": email,
          "CustomFields": [
                    "Checkout=2018-09-07"
                    ]
          }

values = json.dumps(values)
Sign up to request clarification or add additional context in comments.

Comments

0

The first value in the dictionary initializer is bad syntax - should be something line ”Name”: str(name), Similarly ”Email”: str(email),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.