0

I get json as response as:

{"access_token":"QVQ6YmNlZjI0fdsfsFSZiLWE0OTgtZGUwMTJhMDdjMjYz","token_type":"Bearer","expires_in":7776000}

How do I write to a variable just one value from response?
Need "access_token" key value (= QVQ6YmNlZjI0fdsfsFSZiLWE0OTgtZGUwMTJhMDdjMjYz)?
Also should I better use import requests and if so, how this code would look in there?
Code: (New code with title fixed problem)

import http.client
import json

#Request Client Credential Grant (CCG) token
conn = http.client.HTTPSConnection("sandbox.handelsbanken.com")

payload = "client_id=45325132-fsafsa-saczx&grant_type=client_credentials&scope=AIS"

headers = {
    'Accept': "application/json",
    'Content-Type': "application/x-www-form-urlencoded",
    'content-type': "application/x-www-form-urlencoded",
    'accept': "application/json"
    }

conn.request("POST", "/openbanking/oauth2/token/1.0", payload, headers)

res = conn.getresponse()
data = res.read()
y = json.loads(data)

access_token = y.get("access_token", None) 

#print(data.decode("utf-8"))

Next problem is implementing it in new headers

payload = "{\"access\":\"ALL_ACCOUNTS\"}"

headers = { 'X-IBM-Client-Id': "REPLACE_THIS_VALUE", '
Authorization': "Bearer "+access_token, '
Country': "REPLACE_THIS_VALUE", '
TPP-Transaction-ID': "REPLACE_THIS_VALUE", '
TPP-Request-ID': "REPLACE_THIS_VALUE", '
content-type': "application/json", '
accept': "application/json" }

Do I do it like this?
I understood that I can't comprehend how do another thing with json..

{
  "accounts": [
    {
      "accountId": "ae57e780-6cf3-11e9-9c41-e957ce7d7d69",
      "iban": "SE5460000000000403333911",
      "bban": "403333911",
      "currency": "SEK",
      "accountType": "Allkortskonto",
      "bic": "HANDSESS",
      "clearingNumber": "6295",
      "ownerName": "Bo Bankkund",
      "_links": {
        "transactions": {
          "href": "https://sandbox.handelsbanken.com/openbanking/psd2/v2/accounts/ae57e780-6cf3-11e9-9c41-e957ce7d7d69/transactions"
        }
      }
    },
    {
      "accountId": "ae5835a0-6cf3-11e9-9c41-e957ce7d7d69",
      "iban": "SE8160000000000401975231",
      "bban": "401975231",
      "currency": "SEK",
      "accountType": "Allkonto Ung",
      "bic": "HANDSESS",
      "clearingNumber": "6295",
      "name": "Almas konto",
      "ownerName": "Alma Bankkund",
      "_links": {
        "transactions": {
          "href": "https://sandbox.handelsbanken.com/openbanking/psd2/v2/accounts/ae5835a0-6cf3-11e9-9c41-e957ce7d7d69/transactions"
        }
      }
    }
  ]
}

How to get first "accountId" key value in a variable?

akfas
  • 41
  • 4
  • Note: I changed client_id to something not working. So ask if you need working value to test if your solution for my problem is working. – akfas May 25 '22 at 17:05
  • You can access JSON key, value pair using `[]` or using a method called `get()`. If you want to get the value using `[]` do `access_token = data.decode("utf-8")["access_token"]` if you are not sure if the value is alway there I'd recommend using `get()` since you can pass a fallback value like so `access_token = data.decode("utf-8").get("access_token", None)`, it will set `None` if key is not found. Make sure you are getting JSON object if not try using `json.loads` method from `json` package. – Arslan Sohail Bano May 25 '22 at 17:13
  • @ArslanSohailBano I added last problem in main post. Do I do it like this? I will test a bit if it works as of now, i have different values for some reason. – akfas May 25 '22 at 18:07
  • Have you tried executing it? – Arslan Sohail Bano May 25 '22 at 18:16
  • @Arslan Sohail Bano I did it and it somewhat works but i'm not sure if something is wrong as it gives different result on postman. (even when I add everything correctly, manually) – akfas May 25 '22 at 18:24
  • Ok, so I'm guessing you did get the `access_token` working, so to access first `accountId` you can do `data['accounts'][0]['accountId']`, first you get the `accounts` which is an array of objects, then you get the first value inside that array using the index `0` and finally you will be able to get the `accountId` key, make sure to check if there is data coming in `accounts` key using the `length` function. – Arslan Sohail Bano May 26 '22 at 12:56

1 Answers1

2

1.How do I write to a variable just one value from response?:

data = res.read()
y = json.loads(data) #transforms "data" to json

access_token = y.get("access_token", None) #access "access_token" value and if there's no value makes it none with "None"
  1. Next problem is implementing it in new headers:
headers = { 'X-IBM-Client-Id': "REPLACE_THIS_VALUE", '
Authorization': "Bearer "+access_token, '
Country': "REPLACE_THIS_VALUE", '
TPP-Transaction-ID': "REPLACE_THIS_VALUE", '
TPP-Request-ID': "REPLACE_THIS_VALUE", '
content-type': "application/json", '
accept': "application/json" }
  1. How to get first "accountId" key value in a variable?
data = res.read()
y = json.loads(data)
accounts = y["accounts"][0] # saves in variable first accounts array elements
accountId = accounts["accountId"] #from accounts variable takes "accountId" value
akfas
  • 41
  • 4
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – lemon Jun 03 '22 at 22:39
  • okay, I will explain in more detail. – akfas Jun 03 '22 at 22:42
  • Good job! A deserved +1 for a well written answer. – lemon Jun 03 '22 at 22:56