Abdellah
Abdellah

Reputation: 73

Python | NameError: name 'null' is not defined

i have a json response as a string inside a json list as you in the picture enter image description here

i trying to get the value inside the string i tired to use eval() but output shows me this error NameError: name 'null' is not defined i can't read the json values when they are a string enter image description here

this is my code :

 url = "https://api.pipedream.com/v1/sources/code/event_summaries? 
 expand=event"
 headers = {"Authorization": "Bearer hash "}
 response = requests.get(url, headers=headers)
 data = response.text
 datas = json.loads(data)
 darts = datas['data']
 for i in darts:
   trake = i['event']['body']
       for docz in trake:
         open_time = open_time = docz['open_time']
         print(open_time)

enter image description here

the problem is the json values are string i cannot detect values

By the way the Bearer Authorization is just a demo

Upvotes: 2

Views: 11305

Answers (1)

Harshana
Harshana

Reputation: 5486

The data you needed is inside a dict key. So, you need to use .keys() attribute to retrieve it and then you have to use json.loads() to convert it to a dictionary.

Please check the below code:

import requests
import http.client
import json
from ast import literal_eval as evall


url = "https://api.pipedream.com/v1/sources/code/event_summaries?expand=event"
headers = {"Authorization": "Bearer hash"}
response = requests.get(url, headers=headers)


data = response.text
datas = json.loads(data)

darts = datas['data']

for i in darts:
    trake = i['event']['body']
    for docz in trake:
        print(docz)


for tracks in darts:
    tracks = json.loads(list(tracks['event']['body'].keys())[0])
    print(tracks)
    
    open_time = tracks['event']['trade'].get('open_time', '')
    close_time = tracks['event']['trade'].get('close_time', '')
    Lots = tracks['event']['trade'].get('lots', '')
    balance = tracks['event']['account'].get('balance', '')
    symbol = tracks['event']['trade'].get('symbol', '')
    profit = tracks['event']['trade'].get('profit', '')
    total_profit = tracks['event']['trade'].get('total_profit', '')


    msg = """

      Open time :  """ +open_time + """
      Close time :  """ +close_time + """
      Symbol : """ +symbol + """
      lots : """ +Lots + """
      Balance : """ +balance + """
      """


    print(msg)
    print("success")

Upvotes: 4

Related Questions