0

I am trying to read json response from url in python. the below code works fine but the problem is i need to grab the key based on the subject say if subject is "Indices Daily level" then it should print the following key hkr1omlsnteodhkvnt98q20682ghv1fmegb8de01 enter image description here

import json, pandas as pd
import urllib

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/search"
response = urllib.request.urlopen(URL)
text = response.read()
json_data = json.loads(text)
print(json_data)
15
  • next(d["key"] for d in json_data if d["subject"] == "Indices Daily level") ? Commented Jul 19, 2022 at 18:18
  • Hi Olvin, thanks for the suggestion, may i pls know what is d here ? Commented Jul 19, 2022 at 18:27
  • getting stop iteration error Commented Jul 19, 2022 at 18:29
  • 1
    print(next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level")) Commented Jul 19, 2022 at 18:35
  • 1
    Sort it before json_data.sort(key=itemgetter("processed")). Don't forget to import itemgetter(). Commented Jul 19, 2022 at 18:42

3 Answers 3

1

To get first value which match some criteria from list we can pass generator expression which iterates over this list with condition straight into next() which will return first value from passed generator. As you've mentioned in this comment, in case if there are two or more values which matches condition you need to get one which has "latest processed time" which I assume stored in "processed" key of each JSON object in list and contains date in ISO format. To achieve that we can sort list (using list.sort()) in descending order by value of "processed" key (passing itemgetter() as key argument) before lookup. And finally you've mentioned that you need to use extracted "key" in next URL, so you need just concatenate it between two URL path parts you provided.

Code:

import json
from urllib.request import urlopen
from operator import itemgetter

with urlopen('https://pv-ft-marketdata-store.ihsmvals-dev.com/email/search') as resp:
    json_data = json.load(resp)
    
json_data.sort(key=itemgetter("processed"), reverse=True)
key = next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level")

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/" + key + "/data/html"
print(URL)
Sign up to request clarification or add additional context in comments.

2 Comments

now i will be trying to grab html tables from the second URL and store the entire table in dataframe using bs4 import beautifulsoup and lxml
@RahulVaidya, hope you will succeed with this.
0
print(next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level"))
key = str(next(d["key"] for d in json_data if d["subject"] == "Indices Daily Level"))

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/" + 'key'  "/data/html"
print(URL)

8 Comments

not getting the key variable in second URL
Read this comment again. And again. Repeat until understanding comes.
By the way, you don't need to cast return of next() to str as it's already string, just remove str() call. And if you want to print key, just call print(key) after you assigned value to variable, no need to call next() twice.
still trying to convince my mind
Hi Olvin, in case i have a dynamic subject line like "MS strategy daily levels for 2022-07-20", is it possible i only given the subject string till "MS strategy daily levels" and it will fetch its corresponding key ?
|
0
import json
from urllib.request import urlopen
from operator import itemgetter
import pandas as pd
import requests
import re

with urlopen('https://pv-ft-marketdata-store.ihsmvals-dev.com/email/search') as resp:
    json_data = json.load(resp)

json_data.sort(key=itemgetter("processed"), reverse=True)
key = next(d["key"] for d in json_data
if d["subject"].startswith ("Morgan Stanley Systematic Strategies Daily Levels")

URL = "https://pv-ft-marketdata-store.ihsmvals-dev.com/email/" + key + "/data/html"
html = requests.get(URL).content
df_list = pd.read_html(html)
df = df_list[-1]
print(df)

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.