2

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

7
  • Could you post a minimal reproducible example instead of these unaligned chunks of code that do not reproduce the problem you are describing? Commented Nov 18, 2020 at 11:17
  • Man better remove the bearer token Commented Nov 18, 2020 at 11:18
  • Tried to reproduce the problem but copy-pasted code will not work due to indentation issues. Commented Nov 18, 2020 at 11:18
  • @khelwood ok i'll try to give more informations Commented Nov 18, 2020 at 11:19
  • @Abdopy Not more, just better. Commented Nov 18, 2020 at 11:21

1 Answer 1

4

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")
Sign up to request clarification or add additional context in comments.

1 Comment

Glad I could help :)

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.