I am having problems getting some values using jmespath.searc().
I want to get the #value where the '_instrumentIdScheme': 'mhi:MHILIST' If anyone can help, I am now doing this.
prices_id_list = []
f_prices = open(f"{tempdir}\\cdwSecurityPrices.csv").read().replace("\n", "")
json_obj_prices = json.loads(f_prices, strict=False)
for doc in json_obj_prices:
mhilist_n = jmespath.search("assetPricing.instrumentId", doc)
for i, x in enumerate(mhilist_n): # iterate over the instrumentId
mhilist = jmespath.search(
f"assetPricing[{i}].instrumentId[?_instrumentIdScheme == 'mhi:MHILIST']",
doc,
)[0]["#value"]
prices_id_list.append(mhilist)
part of the JSON structure
CodePudding user response:
replace all simple quotes by double quotes
to all #value with the condition:
values = jmespath.search('[]["m:assetPricing"][][]."m:instrumentId"[?"_instrumentIdScheme" == `mhi:MHILIST`].["#value"]', data)
then just flatten the list
# function
def flatten_list(data):
# iterating over the data
flat_list = []
for element in data:
# checking for list
if type(element) == list:
# calling the same function with current element as new argument
flatten_list(element)
else:
flat_list.append(element)
return flat_list
values = flatten_list(values)
print(values)