I am trying to extract a specific column or layer, not sure what you want to call it.. that is inside a json object of which I have converted to what I think is a layered list but I have two problems, my check to see if "return" is in the list is not finding anything even though when printing jsonb I can see it is in the list, my second problem is how do I extract a certain column from a layer.. in this case I need the number "43343243" out the second layer and put into a variable I tried referencing it with jsonb["return"][0] and I got a key error..
My code:
def worker(pairtxt):
while (1 < 2):
balanceobject = requests.post(urlauth, headers=headers, data=paybytes)
json_stringb = str(balanceobject.content, 'utf8')
jsonb = json.loads(json_stringb)
print(jsonb)
if "return" in jsonb: #fails
print(jsonb["return"]["0"]) # key error
print(jsonb["return"]) # prints everything even layers under the number
My jsonb print output
{'success': 1, 'return': {'43343243': {'status': 0, 'pair': 'rgeg',
'type': 'sell', 'amount': 0.01000002, 'rate': 1.0,
'timestamp_created': 1502642258}}}
Because 43343243 is a key not a value, you need to get the keys of return :
jsonb = {'success': 1, 'return': {'43343243': {'status': 0, 'pair': 'rgeg', 'type': 'sell', 'amount': 0.01000002, 'rate': 1.0, 'timestamp_created': 1502642258}}}
numberWanted = list(jsonb['return'].keys())[0]
print(numberWanted) # => 43343243
I think you are looking at the output jsonb as a list (which is not true). jsonb is a dictionary. To verify that you can do this:
print(type(jsonb))
A dictionary has key-value pairs.
success is a key and 1 is the value.
return is a key and the value is another dictionary.
{
'43343243': {
'status': 0,
'pair': 'rgeg',
'type': 'sell',
'amount': 0.01000002,
'rate': 1.0,
'timestamp_created': 1502642258
}
}
If you want to access 43343243, then you can do jsonb['return']['43343243']
Related
The following lists are given:
atr = [{'name': 'surname', 'type': 'varchar(50)', 'table': None}, {'name': 'ls_data', 'type': 'timestamp', 'table': None}, {'name': 'cpn', 'type': 'int', 'table': None}, {'name': 'code', 'type': 'varchar(200)', 'table': None}]
pk = ['surname', 'cpn', 'ls_data']
It is necessary to form a list of "type" from the atr list, while "name" from atr = pk.
The order should be as in the pk list.
Expected output
lst = ['varchar(50)', 'int', 'timestamp']
I tried it like this
lst = [d["type"] for d in atr if d["name"] in pk]
But this is incorrect, the order is not the same as in the pk list.
It would work using something like this:
lst = [atr[[d["name"] for d in atr].index(p)]["type"] for p in pk]
The output for print(lst) is:
['varchar(50)', 'int', 'timestamp']
i.e. the same result items list order as in the query items list as opposed to your original approach which gives a different order.
Though I'm not sure how readable/performant that is; it
generates a new list (only containing the values of the "name" key) from the list of dictionaries for each query item p in pk
searches for the index of the current query item p in that list
uses this index to retrieve the respective dictionary from the original atr list
and finally, select the value of the "type" key from that dictionary
I have a dictionary like below, i want to create a a dataframe but only from result key(i.e a list of dictionary)
dict = {
'ID': 'c2a4da4e3',
'Time': None,
'recordsReturned': 5,
'TotalRecord': 512,
'result': '[{"Time":"2022-03-04T14:39:59.999Z","metric":"dpmo","value":15},
{"Time":"022-03-04T14:39:59.999Z" ","metric":"Load Balancing","value":1},
{"Time":"2022-03-04T14:39:59.999Z","metric":"High Defect","value":13},
{"Time":"2022-03-04T14:39:59.999Z","metric":"Low Defect","value":121},
{"Time":"2022-03-04T14:39:59.999Z","metric":"Successful ","value":306}]',
'Token': None
}
I want something like below.
df = Time metric value
2022-03-04T14:39:59.999Z dpmo 15
022-03-04T14:39:59.999Z Load Balancing 1
i tried this but gives me an empty list of values
print(([key for key in raw.keys()][4], [value for value in raw.values()][4]))
Working on a little project and I may be in over my head. Using the CoinMarketCap API, I am trying to understand how to parse through their results to pull out just specific pieces of the returned value.
As an example:
result = {'status': {'timestamp': '2021-02-22T00:04:51.978Z', 'error_code': 0, 'error_message': None, 'elapsed': 46, 'credit_count': 1, 'notice': None}, 'data': {'1INCH': {'id': 8104, 'name': '1inch', 'symbol': '1INCH', 'slug': '1inch', 'cmc_rank': 83, 'last_updated': '2021-02-22T00:03:09.000Z', 'quote': {'BTC': {'price': 8.793673178965842e-05, 'volume_24h': 4010.9008604493424, 'percent_change_1h': 1.77689058, 'percent_change_24h': -3.76351861, 'percent_change_7d': -19.9798068, 'percent_change_30d': 66.4333541, 'market_cap': 12615.667000751586, 'last_updated': '2021-02-22T00:03:02.000Z'}}}}}
I am unable to figure out how to extract the 'symbol', 'cmc_rank', and 'market_cap' values from this variable. What is the proper approach to doing so?
Thank you
Try this solution, it should give you what you're looking for:
symbol = result['data']['1INCH']['symbol']
cmc_rank = result['data']['1INCH']['cmc_rank']
market_cap = result['data']['1INCH']['quote']['BTC']['market_cap']
Did you try this?
print(result["data"]["1INCH"]["symbol"])
print(result["data"]["1INCH"]["cmc_rank"])
print(result["data"]["1INCH"]["quote"]["BTC"]["market_cap"])
The "{}" indicate a python dict (dictionary) -- (square brackets [] would be a list, and parentheses () would be a tuple). Dicts can also be nested (same with lists and tuples)
In this case, you have a nested dictionar(ies). It's helpful to do some indentation...
result = {
'status': {
'timestamp': '2021-02-22T00:04:51.978Z',
'error_code': 0,
'error_message': None, 'elapsed': 46, 'credit_count': 1, 'notice': None},
'data': {
'1INCH': {
'id': 8104, 'name': '1inch', 'symbol': '1INCH', 'slug': '1inch', 'cmc_rank': 83, 'last_updated': '2021-02-22T00:03:09.000Z',
'quote': {
'BTC': {
'price': 8.793673178965842e-05, 'volume_24h': 4010.9008604493424, 'percent_change_1h': 1.77689058, 'percent_change_24h': -3.76351861, 'percent_change_7d': -19.9798068, 'percent_change_30d': 66.4333541, 'market_cap': 12615.667000751586, 'last_updated': '2021-02-22T00:03:02.000Z'}}}}}
The part to the left of the colon (:) in a dict is the key and the part to the right is the value.
So in the example you gave: result['data']['1INCH']['symbol'] would give you the value of symbol and result['data']['1INCH']['quote']['BTC']['market_cap'] would give you the value of market cap.
HOWEVER, this will only work if the keys don't change. In this case, it looks like the result is coming back with the symbol ('1INCH') as the key. Same with the currency ('BTC'). If you are always expecting '1INCH' and 'BTC' then you can hard code it. On the other hand, if the symbol and/or currency changes you would want to (a) store variables and use those instead (e.g. symbol='1INCH' .... result = x.query(symbol).... result['data'][symbol].....) OR (2) get the keys or (3) loop.
To get a list of the keys of any dictionary -- in this example, the keys of the dictionary 'data' : dkeys = list(result['data'].keys()) ... you can then check the length with len(dkeys) and/or access the key with numbers (since it's a list) dkeys[0]. So, something like result['data'][dkeys[0]]...
Or you could loop - which would be great if you have multiple results:
#the .items() method will return 2 values - the key and value for each entry
for k, v in result['data'].items():
#k would be the symbol in this case and v is the dictionary represented by that key
market_cap = v['quote']['BTC']['market_cap']
#note if there are multiple symbols here, it would overwrite market_cap...
I'm trying to get some parameters of my orders from Python-Binance API.
In this case, I would like to get only the 'status' of an order and save it into a variable.
I used this to get my last order :
orders = client.get_all_orders(symbol=symbol, limit=1)
And I get this result:
[{'clientOrderId': 'HzHxjogJf5rXrzD2uFnTyMn',
'cummulativeQuoteQty': '12.06757100',
'executedQty': '0.00030000',
'icebergQty': '0.00000000',
'isWorking': True,
'orderId': 88978639302,
'orderListId': -1,
'origQty': '0.00030000',
'origQuoteOrderQty': '0.00000000',
'price': '31558.57000000',
'side': 'BUY',
'status': 'FILLED',
'stopPrice': '31592.06000000',
'symbol': 'BTCUSDT',
'time': 1612653434918,
'timeInForce': 'GTC',
'type': 'STOP_LOSS_LIMIT',
'updateTime': 1612109872451}]
Tried to print just the status:
pprint.pprint(orders['status'])
But it returns an error:
TypeError: list indices must be integers or slices, not str
Try this:
pprint.pprint(orders[0]['status'])
order variable is a list that contains dictionary. To get access to first element of the list use [0]. Now you only get value from dictionary. You can do this using [dictionary_key] in this example ['status']. Combining this you can print status.
If you are not sure which type is your variable use simply print(type(variable_name))
I'm trying to iterate through a list and get key and the corresponding values i need like category, current_severity, event, component_name and component_type.
The challenge i am facing is, the content of the list might change, like the key and values will remain the same but the stanzas might be less, zero or more. So i need something more dynamic.
I need to assign the dynamically created variables to prometheus metrics which will also need to be dynamic.
i tried to create dynamic variables like this, it partially works, like it creates duplicates.
the code i tried:-
url = htpp://url
data = json.loads(r.get(url, headers=headers,
verify=False).content.decode('UTF-8'))
var = string.ascii_lowercase
d = {}
k = 0
for value in data:
d[var[k]] = value['software']
k += 1
d[var[k]] = value['current_severity']
k += 1
print(d)
This prints out duplicates like
{'a': 'software', 'b': low}
{'a': 'software', 'b': low, 'c': 'software', 'd': medium}
I want to assign the values to variables and those to be assigned to prometheus metrics. Prometheus metrics are also to be created based on how many ever variables are there.
My desired output:
metric = Metric('prometheus_metric_name', 'prometheus metric name', 'gauge')
metric.add_sample('prometheus_metric_name', value=float(0), labels={'software':(variable of software will go here), 'severity':(variable of current_severity will go here)})
the metric.add_sample line should be created dynamically for how many ever stanzas have been returned. Below is the list returned by data
[{'category': 'software', 'code': 110, 'actual': '["5.1.4"]', 'opened':
'2018-10-16T09:18:12Z', 'component_type': 'update', 'event': 'new update
available', 'current_severity': 'info', 'details': '', 'expected': None,
'id': 10088862, 'component_name': 'Purity//FA'}, {'category': 'software',
'code': 67, 'actual': None, 'opened': '2018-10-18T01:14:45Z',
'component_type': 'host', 'event': 'misconfiguration', 'current_severity':
'critical', 'details': '', 'expected': None, 'id': 10088898,
'component_name': 'pudc-vm-001'}
By stanza i mean - in the above data there are two stanzas. So how many ever there are, i need to get key and value pairs out of it and assign and create metrics as to those.