Sum JSON objects and get a JSON file in Python - python

I have a JSON file with many objects. I want to filter it to discard all the objects that does not have a specific field called ´id´. I developed a piece of code but it does not work:
import json
b=open("all.json","r")
sytems_objs=json.loads(b.read())
flag=0
for i in range(len(sytems_objs)):
if sytems_objs[i]["id"]<>None:
if flag==0:
total=sytems_objs[i]
flag=1
else:
total=total+sytems_objs[i]
file1=open("filtered.json","w+")
json.dump(total, file1)
c=open("filtered.json","r")
sytems_objs2=json.loads(b.read())
I get a Error: ValueError: No JSON object could be decoded
What am I doing wrong?

I'm assuming that system_objs is originally an array of objects
system_objs = json.loads(b.read())
# create a list that only have dicts with the property 'id'
# just read the comment to also include if id is not null
system_objs = [o for o in system_objs if 'id' in o and o['id'] is not None]
# how many dicts have 'id' with it
print len(system_objs)
# write the system objs to a json file
with open('filtered.json', 'w') as f:
f.write(json.dumps(system_objs))

Related

Parse through a JSON file and pick out certain properties

I have a JSON file containing over 8000 features. An example of one is below:
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"uuid":"____","part2":"_____","length":"529","function":"______"},"geometry":{"type":"LineString","coordinates":[[-360909.60698310119,7600968.922204642,0.0],[-361357.344715965,7600811.951385159,0.0],[-361805.08159795138,7600654.939420643,0.0]]}}
I am trying to use python to iterate through the features and print out certain aspects from the properties. At the minute I have the following code which is iterating through the features and printing the 1st feature and its properties 8420 times.
import json
# Opening JSON file
f = open('file.json')
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['features']:
print(data["features"][0]["properties"])
How can I amend the above to show the features and properties for each of the 8420 features? I have tried
for i in data['features']:
print(data["features"][i]["properties"])
but I get the error TypeError: list indices must be integers or slices, not dict
i is a dictionary, not an index.
for i in data['features']:
print(i["properties"])
If it was important to you to use an index, use enumerate or range:
for i, d in enumerate(data['features']):
# data['features'][i] == d
for i in range(len(data['features'])):
# data['features'][i] ...

How to get complete dictionary data from a JSON file based on a value

I have a json file, which I will read and based on the xyz details will create excel report. Below is the sample json file I will use to extract the information which holds data in format of multiple dictionaries.
Now my requirement is to fetch xyz value one by one and based on it using certain field create a report. Below is the small snippet of the code where I am reading the file and based on key populating results. The data I am referencing after reading it from a file.
def pop_ws(dictionary,ws):
r=1
count=1
for k,v in dictionary.items():
offs=len(v['current'])
ws.cell(row=r+1,column=1).value = k
ws.cell(row=r+1,column=4).value = v['abc']
ws.cell(row=r+1,column=5).value = v['def']
wrk=read_cves(k)
count +=1
if wrk !='SAT':
ws.cell(row=r+1,column=7).value =k
ws.cell(row=r+1,column=8).value =tmp1['public_date']
if 'cvss' in list(tmp1.keys()):
.
.
.
def read_f(data):
with open(dat.json) as f:
wrk = f.read()
I am pretty much stuck on how to code in def read_f(data):, so that it read dat.json and based on value i.e data, fetch details defined as in dictionary structure one by one for all the required data and populate as defined under pop_ws in my code.
The data in def read_f(data): will be a dynamic value and based on it I need to filter the dictionary which have value (stored in data) defined against a key and then extract the whole dictionary into another json file.
Any suggestion on this will be appreciated.
Use json package to load json format data like below:
# Python program to read
# json file
import json
# Opening JSON file
f = open('data.json',)
# returns JSON object as
# a dictionary
data = json.load(f)
# Iterating through the json
# list
for i in data['emp_details']:
print(i)
# Closing file
f.close()
I got this from this link, now you can get dict from the file.
Next you can just filter the dict with specific value like below.
You should use filter() built-in function, with a function that returns True, if the dictionary contains one of the values.
def filter_func(dic, filterdic):
for k,v in filterdic.items():
if k == 'items':
if any(elemv in dic[k] for elemv in v):
return True
elif v == dic[k]:
return True
return False
def filter_cards(deck, filterdic):
return list(filter(lambda dic, filterdic=filterdic: filter_func(dic, filterdic) , deck))
You should use a dictionary as the second element.
filter_cards(deck, {'CVE': 'moderate'})
Hopefully, this could helpful for your situation.
Thanks.
Once you get your json object, you can access each value using the key like so:
print(json_obj["key"]) #prints the json value for that key
In your case
print(wrk["CVE"]) # prints CVE-2020-25624

Retrieving attributes of objects stored in dictionary

class Customer:
def __init__(self,custid,name,addr,city,state,zipcode):
self.custid=custid
self.name=name
self.addr=addr
self.city=city
self.state=state
self.zipcode=zipcode
self.memberLevel=BasicMember()
self.monthlySpending =0
Well i am able to read a file and then split it such that in dictionary key is my customerid and value is the customer object. But i can't retrieve the attributes for each object stored in my dictionary. How to get each objects attributes from dictionary.
for line in open('customers.dat','r'):
item=line.rstrip(',')
intput =line.split(',')
cc=Customer.Customer(*intput)
s2=item.split(',',1)[0]
d[s2]=[cc]
sample data of customer is :
619738273,Admiral Ackbar,383 NeiMoidian Road,Utapau,MA,01720
118077058,Padme Amidala,846 Amani Road,D'Qar,MA,01508
360513913,Wedge Antilles,700 NeiMoidian Road,D'Qar,MA,01508
while my output after storing each object in dictionary is :
{'739118188': [<Customer.Customer object at 0x005FF8B0>],
'578148567': [<Customer.Customer object at 0x005FF9B0>]}
So how to get attributes for the object stored in the dictionary.
I'm not sure why you wrapped each one in a list, but simply access them as normal:
>>> d['619738273'][0].name
'Admiral Ackbar'
I'd recommend not wrapping each one in a list:
d[s2] = cc
Then you don't need the [0]:
>>> d['619738273'].name
'Admiral Ackbar'
You can also streamline the parsing step:
with open('customers.dat') as f:
for line in f:
k,*data = line.split(',')
d[k] = Customer.Customer(k, *data)
Although it'd be better to use csv, since it looks like you're working with a CSV file:
import csv
with open('customers.dat') as f:
reader = csv.reader(f)
for k,*data in reader:
d[k] = Customer.Customer(k, *data)

Merging list objects in python

I have a pickle file having many objects. I need to have one proper object by combining all the other objects in the file. How can I do that. I tried using many commands, but none seems to work.
objs = []
while True:
try:
f = open(picklename,"rb")
objs.append(pickle.load(f))
f.close()
except EOFError:
break
Like the one above as shown.
OBJECT stored image :
<nltk.classify.naivebayes.NaiveBayesClassifier object at 0x7fb172819198>
<nltk.classify.naivebayes.NaiveBayesClassifier object at 0x7fb1719ce4a8>
<nltk.classify.naivebayes.NaiveBayesClassifier object at 0x7fb1723caeb8>
<nltk.classify.naivebayes.NaiveBayesClassifier object at 0x7fb172113588>
You should use .extend() to append all items in the list to objs:
(Assuming pickle.load(f) returns a list of objects)
objs.extend(pickle.load(f))

TypeError: string indices must be integers when parsing JSON

This code throws a TypeError: string indices must be integers when parsing JSON. Why is that?
ids=[]
for line in f:
k = json.loads(line)
if "person" in k:
for id in ids:
if k["iden"] == id[0]: #Exception raised here
#Do some processing
else:
ids += [(k["iden"], 1)]
json.loads(line) gives you a string (in your case), not a dictionary. If you've got a dictionary, you can use dictionary['whatever'], but your_str['other_str'] won't work. Check your json file, it may be containing some unwanted data.
Here's the documentation on json.loads(s):
Deserialize s (a str or unicode instance containing a JSON document) to a Python object.
In your case, that Python object is a string, not a dictionary.
I'll guess f is your file.readlines(), and you have something like this before:
my_json_file = open('/path/to/file.json')
f = my_json_file.readlines()
my_json_file.close()
Try, instead of doing readlines(), to pass the file directly to json.load:
my_json_file = open('/path/to/file.json')
k = json.loads(my_json_file)
my_json_file.close()

Categories