1

My program is given a JSON class and I need to access certain items within it.

I don't exactly understand what type of data structure it is, which may be part of my problem.

I've tried json.dumps() and json.load(), both of which return errors. I've even tried ._dict_.

I've received below error:
"the JSON object must be str, bytes or bytearray, not 'LambdaContext'," "'LambdaContext' object has no attribute '_dict_'," and "Object of type 'LambdaContext' is not JSON serializable." I don't know what else to do with this JSON data.

I need to access the "apiAccessToken."

The JSON data:

{
  "context": {
    "System": {
      "apiAccessToken": "AxThk...",
      "apiEndpoint": "https://api.amazonalexa.com",
      "device": {
        "deviceId": "string-identifying-the-device",
        "supportedInterfaces": {}
      },
      "application": {
        "applicationId": "string"
      },
      "user": {}
    }
  }
}

My Code:

def postalCodeRetriever(intent, session, context):
    deviceId = session['user']['userId']
    jsoninfo = json.dumps(context)
    json_dict = json.loads(jsoninfo)
    print(str(json_dict))
    TOKEN = context["System"]
    print(TOKEN)
    URL = "https://api.amazonalexa.com/v1/devices/" + deviceId + "/settings/address/countryAndPostalCode"
    HEADER = {'Accept': 'application/json', 'Authorization': 'Bearer ' + TOKEN}
    response = urllib2.urlopen(URL, headers=HEADER)
    data = json.load(response)
    postalCode = data['postalCode']
    return build_response({}, build_speechlet_response(
                "hello", postalCode, None, True))
2
  • 1
    Can you edit your question and add how do you try to read (parse) that data? Commented Dec 28, 2017 at 6:03
  • 1
    Please post your code Commented Dec 28, 2017 at 6:41

1 Answer 1

4

Below code should do it:

import json
data = json.dumps({
  "context": {
    "System": {
      "apiAccessToken": "AxThk...",
      "apiEndpoint": "https://api.amazonalexa.com",
      "device": {
        "deviceId": "string-identifying-the-device",
        "supportedInterfaces": {}
      },
      "application": {
        "applicationId": "string"
      },
      "user": {}
    }
  }
})
data_dict = json.loads(data)
print(data_dict['context']['System']['apiAccessToken'])

Output:

AxThk...
Sign up to request clarification or add additional context in comments.

1 Comment

TypeError: Object of type 'LambdaContext' is not JSON serializable

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.