Trying to parse JSON data with python - python

I am having no luck trying to parse this json data, i only care about a small amount of it.
json data
{
"timestamp" : 1397555135361,
"sets" : {
"worldguard.markerset" : {
"areas" : {
"world_region_name" : {
"markup" : false,
"desc" : "What I really want.",
"weight" : 3,
"color" : "#FF0000",
"fillopacity" : 0.35,
"opacity" : 0.8,
"label" : "Region_name",
"ytop" : 65.0,
"fillcolor" : "#FF0000",
"z" : [846.0, 847.0, 847.0, 846.0],
"ybottom" : 65.0,
"x" : [773.0, 773.0, 774.0, 774.0]
}
}
}
}
}
I hope I copied it correctly, it a very large file, and I only care about the region info that it has.
there are other parts of this json file, that I don't care about, so I haven't included them. but there are many items under 'areas' that I do care about. I just cant work out how to parse them all
import json
from pprint import pprint
json_data=open('marker_world.json')
data = json.load(json_data)
for item in data["sets"]["worldguard.markerset"]["areas"]:
print item
the items that i care about from each region is; desc, label, z, & x .
It doesn't seem to print out the everything under that region like I would expect all I get is a screen of "u'w'"
I haven't even started to try and select only the bits out of each region I care about. A push in the right direction would be great if you can workout what I am doing wrong.

Here's what you can start with.
Define a list of keys you need from an area, then iterate over areas, for each area get the values of the keys you've defined:
keys = ['desc', 'label', 'x', 'z']
for area_key, area_items in data["sets"]["worldguard.markerset"]["areas"].iteritems():
print area_key
for key in keys:
print '%s: %s' % (key, area_items[key])
prints:
world_region_name
desc: What I really want.
label: Region_name
x: [773.0, 773.0, 774.0, 774.0]
z: [846.0, 847.0, 847.0, 846.0]

Related

Get the value of specific key from JSON file python

I have a JSON file containing the list of price changes of all cryptocurrencies
I want to extract all 'percentage' for all the coins.
Using the code below it throws TypeError: string indices must be integers (which I know is totally wrong, Basically trying to understand how can I search for percentage and get its value for all items)
with open('balance.txt') as json_file:
data = json.load(json_file)
for json_i in data:
print(json_i['priceChangePercent'])
Any help is appreciated
I have attached the json file hereJSON FILE
Below is the sample of JSON file for those who dont want to open link
{
"ETH/BTC":{
"symbol":"ETH/BTC",
"timestamp":1630501910299,
"datetime":"2021-09-01T13:11:50.299Z",
"open":0.071579,
"close":0.0744,
"last":0.0744,
"previousClose":0.071585,
"change":0.002821,
"percentage":3.941,
"average":null,
"baseVolume":178776.0338,
"quoteVolume":13026.89979053,
"info":{
"symbol":"ETHBTC",
"priceChange":"0.00282100",
"priceChangePercent":"3.941",
"count":"279051"
}
},
"LTC/BTC":{
"symbol":"LTC/BTC",
"timestamp":1630501909389,
"datetime":"2021-09-01T13:11:49.389Z",
"open":0.003629,
"close":0.00365,
"last":0.00365,
"previousClose":0.003629,
"change":2.1e-05,
"percentage":0.579,
"average":null,
"baseVolume":132964.808,
"quoteVolume":485.12431556,
"info":{
"symbol":"LTCBTC",
"priceChange":"0.00002100",
"priceChangePercent":"0.579",
"count":"36021"
}
},
"BNB/BTC":{
"symbol":"BNB/BTC",
"timestamp":1630501910176,
"datetime":"2021-09-01T13:11:50.176Z",
"open":0.009848,
"close":0.010073,
"last":0.010073,
"previousClose":0.009848,
"change":0.000225,
"percentage":2.285,
"average":null,
"baseVolume":220645.713,
"quoteVolume":2187.75954249,
"info":{
"symbol":"BNBBTC",
"priceChange":"0.00022500",
"priceChangePercent":"2.285",
"count":"130422"
}
},
If it is single dictionary, it could be done the following way:
data['LTC/BTC']['info']['priceChangePercent']
Extract it using list comprehension.
percentage_list = [value['percentage'] for value in data.values()]
priceChangePercent_list = [value['info']['priceChangePercent'] for value in data.values()]
print(percentage_list)
print(priceChangePercent_list)
[3.941, 0.579, 2.285]
['3.941', '0.579', '2.285']
try this bro
t = []
for key, value in a.items():
if "info" in value and "priceChangePercent" in value["info"]:
t.append(value["info"]["priceChangePercent"])

Sort a json file in python

I had a list of single long string and I wanted to print the output in a particular form.
convert list to a particular json in python
but after conversion order of data changed. How can I maintain the same order?
input_data =
[
"21:15-21:30 IllegalAgrumentsException 1,
21:15-21:30 NullPointerException 2,
22:00-22:15 UserNotFoundException 1,
22:15-22:30 NullPointerException 1
....."
]
Code to covert the data in particular json form:
input_data = input[0] // input is list of single long string.
input_data = re.split(r',\s*', input_data)
output = collections.defaultdict(collections.Counter)
# print(output)
for line in input_data:
time, error, count = line.split(None, 2)
output[time][error] += int(count)
print(output)
response = [
{
"time": time,
"logs": [
{"exception": exception, "count": count}
for (exception, count) in counter.items()
],
}
for (time, counter) in output.items())
]
print(response)
My output :
{
"response": [
{
"logs": [
{
"count": 1,
"exception": "UserNotFoundException"
}
],
"time": "22:45-23:00"
},
{
"logs": [
{
"count": 1,
"exception": "NullPointerException"
}
],
"time": "23:00-23:15"
}...
]
}
so my order is changed but I need my data to be in same order i.e start from 21:15-21:30 and so on.. How can I maintain the same order ?
Your timestamps are already sortable, so if you don't care about the order of individual exceptions, you can just do:
for (time, counter) in sorted(output.items())
which will do a lexicographical sort by time and then by count. You can do sorted(output.items(), key=lambda x: x[0]) if you want just sort by time, or key=lambda x: x[0], -x[1] for by time and count descending.
The data is read into a dictionary, a defaultdict to be precise:
output[time][error] += int(count)
This data structure is grouping the data by time and by error type, which implies that there may be multiple items with the same time and the same error time. There is no way to have the "same order", if the data is regrouped like that.
On the other hand, you probably expect the time to be ordered in the input and even if it is not, you want output ordered by time, yo sou just need to do that, so instead of this:
for (time, counter) in output.items()
do this:
for time in sorted(output)
and then get the counter as
counter = output[time]
EDIT: time is sorted, but not starting at 0:00, sorting by time string is not correct. Instead, sorting the time by the original time order is correct.
Therefore, remember the original time order:
time_order = []
for line in input_data:
time, error, count = line.split(None, 2)
output[time][error] += int(count)
time_order.append(time)
Then later sort by it:
for time in sorted(output, key=time_order.index)

Python Key Value Error (Json)

I am trying to grab this data and print into a string of text i am having the worst! issues getting this to work.
Here is the source i am working with to get a better understanding i am working on an envirmental controller and my sonoff switch combined
https://github.com/FirstCypress/LiV/blob/master/software/liv/iotConnectors/sonoff/sonoff.py this code works for two pages once completed so ignore the keys for tempature etc
m = json.loads(content)
co2 = m["Value"]
I need the value of "Value" under the "TaskValues" it should be either a 1 or a 0 in almost any case how would i pulled that key in the right form?
"Sensors":[
{
"TaskValues": [
{"ValueNumber":1,
"Name":"Switch",
"NrDecimals":0,
"Value":0
}],
"DataAcquisition": [
{"Controller":1,
"IDX":0,
"Enabled":"false"
},
{"Controller":2,
"IDX":0,
"Enabled":"false"
},
{"Controller":3,
"IDX":0,
"Enabled":"false"
}],
"TaskInterval":0,
"Type":"Switch input - Switch",
"TaskName":"relias",
"TaskEnabled":"true",
"TaskNumber":1
}
],
"TTL":60000
}
You can get it by
m['Sensors'][0]['TaskValues'][0]['Value']
"Value" is nested in your json, as you've mentioned. To get what you want, you'll need to traverse the parent data structures:
m = json.loads(content)
# This is a list
a = m.get('Sensors')
# This is a dictionary
sensor = a[0]
# This is a list
taskvalue = sensor.get('TaskValues')
# Your answer
value = taskvalue[0].get('Value')

Raster: How to get elevation at lat/long using python?

I also posted this question in the GIS section of SO. As I'm not sure if this rather a 'pure' python question I also ask it here again.
I was wondering if anyone has some experience in getting elevation data from a raster without using ArcGIS, but rather get the information as a python list or dict?
I get my XY data as a list of tuples.
I'd like to loop through the list or pass it to a function or class-method to get the corresponding elevation for the xy-pairs.
I did some research on the topic and the gdal API sounds promising. Can anyone advice me how to go about things, pitfalls, sample code? Other options?
Thanks for your efforts, LarsVegas
I recommend checking out the Google Elevation API
It's very straightforward to use:
http://maps.googleapis.com/maps/api/elevation/json?locations=39.7391536,-104.9847034&sensor=true_or_false
{
"results" : [
{
"elevation" : 1608.637939453125,
"location" : {
"lat" : 39.73915360,
"lng" : -104.98470340
},
"resolution" : 4.771975994110107
}
],
"status" : "OK"
}
note that the free version is limited to 2500 requests per day.
We used this code to get elevation for a given latitude/longitude (NOTE: we only asked to print the elevation, and the rounded lat and long values).
import urllib.request
import json
lati = input("Enter the latitude:")
lngi = input("Enter the longitude:")
# url_params completes the base url with the given latitude and longitude values
ELEVATION_BASE_URL = 'http://maps.googleapis.com/maps/api/elevation/json?'
URL_PARAMS = "locations=%s,%s&sensor=%s" % (lati, lngi, "false")
url=ELEVATION_BASE_URL + URL_PARAMS
with urllib.request.urlopen(url) as f:
response = json.loads(f.read().decode())
status = response["status"]
result = response["results"][0]
print(float(result["elevation"]))
print(float(result["location"]["lat"]))
print(float(result["location"]["lng"]))
Have a look at altimeter a wrapper for the Google Elevation API
Here is the another one nice API that I`v built: https://algorithmia.com/algorithms/Gaploid/Elevation
import Algorithmia
input = {
"lat": "50.2111",
"lon": "18.1233"
}
client = Algorithmia.client('YOUR_API_KEY')
algo = client.algo('Gaploid/Elevation/0.3.0')
print algo.pipe(input)

Python create list from smallest number of repeating values in a nested list

I have a Dict
whs = {
'ID1' : ['code1', 'code2', 'code3'],
'ID2' : ['code2', 'code5', 'code3'],
'ID3' : ['code6', 'code7', 'code8'],
'ID4' : ['code3', 'code5', 'code6'],
}
What I need to do is build a new list that will look like
submit = [
{
'codes' : ['code3', ],
'ids' : ['ID1', 'ID2', 'ID4'],
},
{
'codes' : ['code6', 'code7', 'code8'],
'ids' : ['ID3', ],
}
]
What I have so far
def ParseAvailable(self, whs):
separate = whs.keys()
submit = []
while len(separate) > 0:
avail = {
'codes' : [],
'ids' : [],
}
for num, item in enumerate(separate):
if len(avail['codes']) == 0:
avail['codes'] = whs[item]
avail['ids'].append(item)
else:
avail_all = list(set(avail['codes']) & set(whs[item]))
print '%s : %s' % (item, avail_all)
if len(avail_all) > 0:
avail['codes'] = avail_all
avail['ids'].append(item)
if len(avail['codes']) > 0:
del separate[num]
submit.append(avail)
return submit
Which returns:
[
{
'ids': ['ID4', 'ID3'],
'codes': ['code6']
},
{
'ids': ['ID2'],
'codes': ['code2', 'code5', 'code3']
},
{
'ids': ['ID1'],
'codes': ['code1', 'code2', 'code3']
}
]
which COULD work except that ID1 & ID2 should be combined as
{
'ids' : ['ID1', 'ID2',],
'codes' : ['code2', 'code3', ]
}
Curious if there is an easier approach that I've not thought of, figure I could setup a couple more nested loops to compare everything piece by piece though it seems rather unpythonic
Thank You in Advance
I attacked it by building a tree of all the potential additions, and then finding the cheapest option among those. Here is a working (albeit ugly and unoptimized) example:
https://gist.github.com/1288835
The tree will end up with p*w nodes, where p is the number of products and w(p) is the average number of warehouses per product.
So it looks to me like you have a set cover problem, which is NP-complete. Not only that, but it appears that you want all the possible set covers of the minimum size. Your list of ids creates the universe set, and each of your codes creates a sub set of the universe.
For your simple iterative loops you don't really have a choice other then to try all possible combinations of codes. If you have large data sets this isn't going to work.
The wikipedia article mentions an efficient algorithm (greedy algorithm) that isn't guaranteed to find the best solution, but the error in the solution is bounded. This algorithm is to iteratively add the subset the contains the most uncovered elements of the universe.
If you need to get the absolutely correct answer you may want to try the Integer Programming approach (as in Linear Programming). There are software packages and libraries for this algorithm.
As others said, this is an optimization problem that isn't trivial for large datasets. But as a first step, it's at least easy to invert the list of warehouses per product to a list of products per warehouse.
whs = {
'ID1' : ['code1', 'code2', 'code3'],
'ID2' : ['code2', 'code5', 'code3'],
'ID3' : ['code6', 'code7', 'code8'],
'ID4' : ['code3', 'code5', 'code6'],
}
a = []
b = []
for k,v in whs.items():
for j in v:
a.append((j,k))
b.append((j,[]))
inv = dict(b)
for j in a:
inv[j[0]].append(j[1])
print("Inventory:")
for k,v in inv.items():
print(k,v)
When run, this prints:
Inventory:
code1 ['ID1']
code2 ['ID2', 'ID1']
code3 ['ID4', 'ID2', 'ID1']
code5 ['ID4', 'ID2']
code6 ['ID4', 'ID3']
code7 ['ID3']
code8 ['ID3']
As you can see from the trivial example, there are no solutions that will get all products in a single order, and several solutions (code2+code6, code3+code6, code3+code7, code3+code8) that will get them in two orders. Picking the "best" would require additional information, such as pricing, to optimize on.

Categories