Background:
I have the following example data structure in JSON:
{'sensor' : [
{'assertions_enabled': 'ucr+',
'deassertions_enabled': 'ucr+',
'entity_id': '7.0',
'lower_critical': 'na',
'lower_non_critical': 'na',
'lower_non_recoverable': 'na',
'reading_type': 'analog',
'sensor_id': 'SR5680 TEMP (0x5d)',
'sensor_reading': {'confidence_interval': '0.500',
'units': 'degrees C',
'value': '42'},
'sensor_type': 'Temperature',
'status': 'ok',
'upper_critical': '59.000',
'upper_non_critical': 'na',
'upper_non_recoverable': 'na'}
]}
The sensor list will actually contain many of these dicts containing sensor info.
Problem:
I'm trying to query the list using jsonpath to return me a subset of sensor dicts that have sensor_type=='Temperature' but I'm getting 'False' returned (no match). Here's my jsonpath expression:
results = jsonpath.jsonpath(ipmi_node, "$.sensor[?(#.['sensor_type']=='Temperature')]")
When I remove the filter expression and just use "$.sensor.*" I get a list of all sensors, so I'm sure the problem is in the filter expression.
I've scanned multiple sites/posts for examples and I can't seem to find anything specific to Python (Javascript and PHP seem to be more prominent). Could anyone offer some guidance please?
The following expression does what you need (notice how the attribute is specified):
jsonpath.jsonpath(impi_node, "$.sensor[?(#.sensor_type=='Temperature')]")
I am using jsonpath-ng which seems to be active (as of 23.11.20) and I provide solution based on to Pedro's jsonpath expression:
data = {
'sensor' : [
{'sensor_type': 'Temperature', 'id': '1'},
{'sensor_type': 'Humidity' , 'id': '2'},
{'sensor_type': 'Temperature', 'id': '3'},
{'sensor_type': 'Density' , 'id': '4'}
]}
from jsonpath_ng.ext import parser
for match in parser.parse("$.sensor[?(#.sensor_type=='Temperature')]").find(data):
print(match.value)
Output:
{'sensor_type': 'Temperature', 'id': '1'}
{'sensor_type': 'Temperature', 'id': '3'}
NOTE: besides basic documentation provided on project's homepage I found additional information in tests.
Related
I am trying to extract values from Graph database. i am trying with the below gremlin console command but it is returning key value pairs, we can convert this to list.
%%gremlin
g.V().hasLabel('airport').limit(2).project('id','label','region','country').by(id()).by(label()).by('region').by('country').fold()
output
[{'id': '1', 'label': 'airport', 'region': 'US-GA', 'country': 'US'}, {'id': '2', 'label': 'airport', 'region': 'US-AK', 'country': 'US'}]
Expected output:
'1', 'airport', 'US-GA', 'US'
'2', 'airport', 'US-AK', 'US'
or
[['1','airport','US-GA','US'], ['2','airport', 'US-AK','US']]
Rather than use project you can use values. Steps like project and valueMap return a key:value map, whereas values does not include the keys in its result.
gremlin> g.V().
hasLabel('airport').
limit(2).
local(union(id(),label(),values('region','country')).fold())
==>[1,airport,US,US-GA]
==>[2,airport,US,US-AK]
As an alternative you can just add a select(values) to your current query which I think I prefer as it avoids needing the local and union steps.
gremlin> g.V().
hasLabel('airport').
limit(2).
project('id','label','region','country').
by(id()).
by(label()).
by('region').by('country').
select(values).
fold()
==>[[1,airport,US-GA,US],[2,airport,US-AK,US]]
I got results through results = requests.get(url).json()
results look like this:
{'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties':
{'name': 'EPSG:4326'}}, 'features': [{'type': 'Feature',
'properties': {'kode': '0101',
'navn': 'København',
'region_kode': '1084.0',
'region_navn': 'Hovedstaden'}, 'bbox': [12.453042062098154,
55.612994971371606,
12.734252598475942,
55.732491190632494]}]}
with results['features'], I am getting this
[{'type': 'Feature', 'properties': {'kode': '0101', 'navn':
'København', 'region_kode': '1084.0', 'region_navn':
'Hovedstaden'}, 'bbox': [12.453042062098154,
55.612994971371606,
12.734252598475942,
55.732491190632494]}]
I want to get the information in navn
and I tried all combination of
results['features']['properties']['navn']
results['features']['navn']
results['features']['properties']
they all show the same error message that: list indices must be integers or slices, not str
apparently, results['features'] is a list with a length of 1.
how can I get to navn information?
I want to make several calls as you can imagine.
The results['features']object is a list, try results['features'][0]['properties']['navn']
Now you select the first element in the list (0), the dictionary, and from that dictionary you select the 'navn' key. The result is the value of 'navn'
Note that python lists are between [] and items are seperated by a comma and python dictionaries are between {} and consists of key, value pairs seperated by a comma.
try this
results['features'][0]['properties']['navn']
You can try code below:
results['features'][0]['properties']['navn']
You should try accessing the first element of the list in result['features'], i.e.:
results['features'][0]['properties']['navn']
Full code:
results = {'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}}, 'features': [{'type': 'Feature',
'properties': {'kode': '0101', 'navn': 'København', 'region_kode': '1084.0', 'region_navn': 'Hovedstaden'}, 'bbox': [12.453042062098154, 55.612994971371606, 12.734252598475942, 55.732491190632494]}]}
print(results['features'][0]['properties']['navn'])
# København
results = {'type': 'FeatureCollection', 'crs': {'type': 'name', 'properties': {'name': 'EPSG:4326'}},
'features': [{'type': 'Feature',
'properties': {'kode': '0101', 'navn': 'København', 'region_kode': '1084.0',
'region_navn': 'Hovedstaden'},
'bbox': [12.453042062098154, 55.612994971371606, 12.734252598475942, 55.732491190632494]}]}
navn = results['features'][0]['properties']['navn']
print(navn)
You got error because inside features there is one list. So, you can not get list with the help of str index and to get the properties inside the features you need to write [0] and the list will gone and you can get the value.
I have a nested dictionary with the following structure. I am trying to convert it to pandas dataframe, however I have problems to split the 'mappings' dictionary to have it in separate columns.
{'16':
{'label': 't1',
'prefLab': 'name',
'altLabel': ['test1', 'test3'],
'map': [{'id': '16', 'idMap': {'ciID': 16, 'map3': '033441'}}]
},
'17':
{'label': 't2',
'prefLab': 'name2',
'broader': ['18'],
'altLabel': ['test2'],
'map': [{'id': '17', 'idMap': {'ciID': 17, 'map1': 1006558, 'map2': 1144}}]
}
}
ideal outcome would be a dataframe with the following structure.
label prefLab broader altLab ciID, map1, map2, map3 ...
16
17
Try with this: assuming your json format name is "data" then
train = pd.DataFrame.from_dict(data, orient='index')
I am new to Python. I have two lists. One is key list and another list is value list.
title = ["Code","Title","Value",....] value = [["100","abcd",100",...],["101","efgh",200",...],...] data={} data.setdefault("data",[]).append({"code": sp[0],"val": sp[2]})
this code gives me the following result.
{'data': [{'code': '100', 'val': '100'},{'code': '101', 'val': '200'}]}
But I want the result as the below,
{ "100": { "Title": "abcd", "Value": "100", ............, ............}, "101": { "Title": "efgh", "Value": "200", ............, ............} }
i.e., The first column of the value list should be the key of every Json array list and other items of the lists should be generated as key and value pair. How can I generate the Json array using Python code referring that two lists.
As it is not mentioned that about the size of list ,the below could would do the job.I am using python3.x
title = ["Code","Title","Value"]
value = [["100","abcd","100"],["101","efgh","200"]]
dic1={}
for i in range(len(title)-1):
for j in range(len(title)-1):
dic1.setdefault(value[i][0],{}).update({title[j+1]:value[i][j+1]})
Output is
{'101': {'Title': 'efgh', 'Value': '200'}, '100': {'Title': 'abcd', 'Value': '100'}}
I hope it is helpful!
You can build a dict with this lists. I made a quick snippet just for you to understand
title = ["Code","Title","Value"]
value = [['100','abcd','100'],['101','efgh','200']]
data={}
for whatever in value:
your_titles = {}
print(whatever[0])
your_titles[title[1]] = whatever[1]
your_titles[title[2]] = whatever[0]
your_titles[title[0]] = whatever[2]
data[whatever[0]] = your_titles
print(data)
The output:
{'100': {'Code': '100', 'Value': '100', 'Title': 'abcd'}, '101': {'Code': '200', 'Value': '101', 'Title': 'efgh'}}
Please read this tutorial and try to make it yourself. This is not the optimal solution for this problem.
Make a data frame and then set the column to index and then convert it to json:
data_frame = pd.DataFrame(columns = title, data = value)
data = data_frame.set_index('Code')
json1 = data.to_json(orient='index')
I am very new to python programming and have yet to buy a textbook on the matter (I am buying one from the store or Amazon today). In the meantime, can you help me with the following problem I have encountered?
I have an list of dictionary objects like this:
stock = [
{ 'date': '2012', 'amount': '1.45', 'type': 'one'},
{ 'date': '2012', 'amount': '1.4', 'type': 'two'},
{ 'date': '2011', 'amount': '1.35', 'type': 'three'},
{ 'date': '2012', 'amount': '1.35', 'type': 'four'}
]
I would like to sort the list by the amount date column and then by the amount column so that the sorted list looks like this:
stock = [
{ 'date': '2011', 'amount': '1.35', 'type': 'three'},
{ 'date': '2012', 'amount': '1.35', 'type': 'four'},
{ 'date': '2012', 'amount': '1.4', 'type': 'two'},
{ 'date': '2012', 'amount': '1.45', 'type': 'one'}
]
I now think I need to use sorted() but as a beginner I am having difficulties understanding to concepts I see.
I tried this:
from operator import itemgetter
all_amounts = itemgetter("amount")
stock.sort(key = all_amounts)
but this resulted in an list that was sorted alphanumerically rather than numerically.
Can someone please tell me how to achieve this seemingly simple sort? Thank-you!
Your sorting condition is too complicated for an operator.itemgetter. You will have to use a lambda function:
stock.sort(key=lambda x: (int(x['date']), float(x['amount'])))
or
all_amounts = lambda x: (int(x['date']), float(x['amount']))
stock.sort(key=all_amounts)
Start by converting your data into a proper format:
stock = [
{ 'date': int(x['date']), 'amount': float(x['amount']), 'type': x['type']}
for x in stock
]
Now stock.sort(key=all_amounts) will return correct results.
As you appear to be new in programming, here's a word of general advice if I may:
Proper data structure is 90 percent of success. Do not try to work around broken data by writing more code. Create a structure adequate to your task and write as less code as possible.
You can also use the fact that python's sort is stable:
stock.sort(key=lambda x: int(x["amount"]))
stock.sort(key=lambda x: int(x["date"]))
Since the items with the same key keep their relative positions when sorting (they're never swapped), you can build up a complicated sort by sorting multiple times.