Home > Enterprise >  How to save json object variable based on specific value
How to save json object variable based on specific value

Time:07-20

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)

CodePudding user response:

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)

You can help my country, check my profile info.

CodePudding user response:

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)

CodePudding user response:

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)
  • Related