Python - How to copy all data out of an array - python

Currently I am exporting a database from firebase into a JSON but it is coming out in as an array.
[{"ConnectionTime": 19.23262298107147, "objectId": "01331oxpVT", "FirmwareRevision": "201504270003 Img-B", "DeviceID": "EDF02C74-6518-489E-8751-25C58F8C830D", "PeripheralType": 4, "updatedAt": "2015-10-09T04:01:39.569Z", "Model": "Bean", "HardwareRevision": "E", "Serial": "Serial Number", "createdAt": "2015-10-09T04:01:39.569Z", "Manufacturer": "Punch Through Design"}, {"ConnectionTime": 0.3193170428276062, "objectId": "018Mv1g6I8", "DeviceID": "42635033-DF3A-4109-A633-C3AB829BE114", "PeripheralType": 2, "updatedAt": "2015-12-08T04:20:41.950Z", "createdAt": "2015-12-08T04:20:41.950Z"}]
And then I get this error - Start of array encountered without start of object.'}]
How can I change this to not be an Array and just a list of data. I also need a line break between each set of data but Im assuming once I get the data out of the array the code I currently have will do that. My code is below. Thanks for the help!
firebase = firebase.FirebaseApplication('https://dataworks-356fa.firebaseio.com/')
result = firebase.get('/connection_info_parse', None)
# id_keys = map(str, result.keys()) #filter out ID names
with open("firetobqrestore1.json", "w") as outfile:
# for id in id_keys:
json.dump(result, outfile, indent=None)
outfile.write("\n")

It sounds like something in your workflow wants newline delimited JSON, although you haven't made it explicitly clear what is giving you this error.
With that caveat, I think this is what you are looking for:
import json
with open("firetobqrestore1.json", "w") as outfile:
for line in result:
json.dump(line, outfile, indent=None)
outfile.write("\n")
This will write individual json objects to each line.
This also assumes that result is an actual python object rather than a JSON string. If it's a string you will need to parse it first with something like:
result = json.loads(result)

If the elements of the list are not parsed ( they are strings), then loop through the list and convert each element to a json using json.loads(). Then, you can use json.dumps()
In case the elements of the list are already parsed,then just loop through the list and use json.dumps().

Related

How to separate key-values per row from a JSON formatted list?

I've been trying to get a more readable output from a JSON list. I have not yet been successful. I hard-coded some data to see if I can get it as I want. This is what I did:
import json
jsonData = {
"person": {"FirstName": "Kwin", "LastName": "Harley", "Age": 25},
"DoB": {"DateOfBirth": "19/12/1996", "Birthplace": "Belgium"},
"insurer":{"id":"12345","contractNumber":"98765432",
"valid_from":"2020-10-01T00:00:00.000Z","valid_until":"2021-01-30T00:00:00.000Z",
"representativeID":"135792468",
"representativeEmail":"sample#test.com"}
}
jsonString = json.dumps(jsonData, sort_keys=False, indent=4)
print(jsonString)
Output 1
As you can see, the data is structured nicely.
Now, when I use my main code, the output looks like this:
Output 2
It just returns the data in 1 row :(
Is there a way to fix that? This is the code I have for that:
qrType = qr.type
qrData = json.dumps(qr.data.decode('utf-8'),sort_keys=True)
# print the QR type and data to the terminal
print("[INFORMATION] Found {} barcode:\n{}".format(qrType, qrData))
I don't think you're passing a dict to json.dumps() at all. qr.data is clearly a string, as you .decode() it. Presumably it's a json string, so you want to do something like this:
formatted_data = json.dumps(json.load(qr.data.decode()), indent=2)
print(formatted_data)

reading from a json file using python

Im trying to read from this json file and print the values. I cant find out how to print all the values from the first (dictonary-index?) in the list.
I want to print the following:
website: https://www.amazon.com/Apple-iPhone-GSM-Unlocked-64GB/dp/B07
price: 382,76
How can i do it?
JSON file:
[
{
"website": "https://www.amazon.com/Apple-iPhone-GSM-Unlocked-64GB/dp/B078P5BK5G",
"price": "382,76"
},
{
"website": "https://www.ebay.com/itm/Apple-iPhone-8-Plus-GSM-Unlocked-64GB-Gold-Renewed-Gold-64-GB-Gold-64-GB-/143340730792",
"price": "609,15"
}
]
Python code:
Tried this
import json
with open('./result.json') as json_file:
data = json.load(json_file)
for p in data:
print(p["price"])
Output is the prices of the products:
382,76
609,15
Instead of printing the prices it should print the values in the first dict in the list. Any good tips on how to do this?
You are looping over the list of dictionaries. If you want to loop over the values of the first dictionary, you first need to get the first element, and loop over that one.
first_dict = data[0]
for value in first_dict.values():
print(value)

How to add a character like ("," , "[" , "]") at the end of a JSON-Object

I have a Python-script that makes an File with invalid JSON.
Now I want to manipulate this JSON-File so it becomes a valid JSON-file by adding a comma between every object, at the beginning of the File a '[' and at the end a ']'.
Is there a way to make this with JSON alone or do i have to find a way with other read and write functions?
Exsample_File.json:
{
"firstName": "Bidhan",
"lastName": "Chatterjee",
"age": 40,
"email":"bidhan#example.com"
}
{
"firstName": "hanbid",
"lastName": "jeeChatter",
"age": 10,
"email":"example#bidhan.com"
}
....
n times
New_File.json:
[
{
"firstName": "Bidhan",
"lastName": "Chatterjee",
"age": 40,
"email":"bidhan#example.com"
},
{
"firstName": "hanbid",
"lastName": "jeeChatter",
"age": 10,
"email":"example#bidhan.com"
},
....
n times
]
This is the function that makes this JSON-File. I dont want to touch the other code where the str is generated.
data = json.loads(str)
with open('Example_File.json','ab')as outfile:
json.dump(data, outfile, indent=2)
So far i dont have an idea to solve this problem. so there is no code sample that would help.
The result should be like the New-File
You may have to read the content as string, manipulate it and load as JSON. Something like this,
import json
with open('Example.json','r') as f:
data = f.read()
data = "[" + data.replace("}", "},", data.count("}")-1) + "]"
json_data = json.loads(data)
It seems your data has numbers begins with 0, so you may ended up with an exception "ValueError". You may refer how to deal the issue from Why is JSON invalid if an integer begins with 0
Note: I manually removed 0 from "Example.json"
Can't you do
words.replace('}','},')
This should replace all instances of '}' with a '},'
First of all I don't think there is a way to parse it directly as JSON array.
However, if your JSON objects are not nested a simple way to parse them is to split your string:
with open(YOUR_FILE) as jsons_file:
jsons = [x.strip() + '}' for x in jsons_file.read().split('}')][:-1]
now you can dump it to file or string using json's library dump or dumps
json.dumps(jsons)
or
with open(OUT_FILE, 'w') as out_file:
json.dump(jsons, out_file)
to add automatic comma between each object and add brackets in file to make it complete json just write a simple jq query
jq -s '.' file_name

Using python to modify format of Json file

I have JSON file that is formatted like this:
(multi-line for clarity)
(line 0001).......
{
"_id": "iD_0001",
"skills": [{
"name": "Project Management"
}, {
"name": "Business Development"
}]
}
....
(line 9999)
{
"_id":"iD_9999",
"skills": [{
"name": "Negotiation"
}, {
"name": "Banking"
}]
}
I'd like to run a program on it, however, the program cannot read it under the aforementioned format.
Thus I'd like to modify its format to:
[{
"_id": "iD_0001",
"skills": [{
"name": "Project Management"
}, {
"name": "Business Development"
}]
},{
"_id":"iD_9999",
"skills": [{
"name": "Negotiation"
}, {
"name": "Banking"
}]
}]
Essentially, putting all entries in a single array.
Is there a way to implement that using Python or demjson?
ALTERNATIVE: I made a program that fetches the skills in these json files and sends them to a text file (Test.txt), however it only works for the second format, not the first. Can you suggest a modification to make it work for the first format (above)?
This is my program:
import json
from pprint import pprint
with open('Sample.json') as data_file:
data = json.load(data_file)
with open('Test.txt', 'w') as f:
for x in data:
for y in x["skills"]:
f.write(y["name"])
f.close()
SOLUTION
Thank you to Antti Haapala for noticing the catenation of Json objects under the first format, as well as to Walter Witzel and Josh J for suggesting alternative answers.
Since the first format is a catenation of individual objects, the program functions well if we load the first Json file Line-by-Line instead of as a whole. I have done that with:
data = []
with open('Sample1-candidats.json') as data_file:
for line in data_file:
data.append(json.loads(line))
with open('Test.txt', 'w') as f:
for x in data:
for y in x["skills"]:
f.write(y["name"])
f.close()
Here it goes. This assumes that your file is just a bunch of individual json objects concatenated and you need to transform in a list of json objects.
import json
from pprint import pprint
with open('sample.json') as data_file:
strData = '[' + ''.join(data_file.readlines()).replace('}\n{','},{') + ']'
data = eval(strData)
with open('Test.txt', 'w') as f:
for x in data:
for y in x["skills"]:
f.write(y["name"])
Here are the steps you can take to accomplish your problem. Since it kinda sounds like a homework assignment, I will give you the logic and pointers but not the code.
Open the file for reading
Read file into string variable (if small enough for memory limits)
Create empty list for output
Split string on .....
json.loads each piece of resulting list
Append each result to your empty output list
Have a cup of coffee to celebrate

how to delete json object using python?

I am using python to delete and update a JSON file generated from the data provided by user, so that only few items should be stored in the database. I want to delete a particular object from the JSON file.
My JSON file is:
[
{
"ename": "mark",
"url": "Lennon.com"
},
{
"ename": "egg",
"url": "Lennon.com"
}
]
I want to delete the JSON object with ename mark.
As I am new to python I tried to delete it by converting objects into dict but it is not working. Is there any other way to do it?
i tried this one:
index=0
while index < len(data):
next=index+1
if(data[index]['ename']==data[next]['ename']):
print "match found at"
print "line %d and %d" %(next,next+1)
del data[next]
index +=1
Here's a complete example that loads the JSON file, removes the target object, and then outputs the updated JSON object to file.
#!/usr/bin/python
# Load the JSON module and use it to load your JSON file.
# I'm assuming that the JSON file contains a list of objects.
import json
obj = json.load(open("file.json"))
# Iterate through the objects in the JSON and pop (remove)
# the obj once we find it.
for i in xrange(len(obj)):
if obj[i]["ename"] == "mark":
obj.pop(i)
break
# Output the updated file with pretty JSON
open("updated-file.json", "w").write(
json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
)
The main point is that we find the object by iterating through the objects in the loaded list, and then pop the object off the list once we find it. If you need to remove more than one object in the list, then you should store the indices of the objects you want to remove, and then remove them all at once after you've reached the end of the for loop (you don't want to modify the list while you iterate through it).
The proper way to json is to deserialize it, modify the created objects, and then, if needed, serialize them back to json.
To do so, use the json module. In short, use <deserialized object> = json.loads(<some json string>) for reading json and <json output> = json.dumps(<your object>) to create json strings.
In your example this would be:
import json
o = json.loads("""[
{
"ename": "mark",
"url": "Lennon.com"
},
{
"ename": "egg",
"url": "Lennon.com"
}
]""")
# kick out the unwanted item from the list
o = filter(lambda x: x['ename']!="mark", o)
output_string = json.dumps(o)
Your json file contains in a list of objects, which are dictionaries in Python. Just replace the list with a new one that doesn't have the object in it:
import json
with open('testdata.json', 'rb') as fp:
jsondata = json.load(fp)
jsondata = [obj for obj in jsondata if obj['ename'] != 'mark']
print(json.dumps(jsondata, indent=4))
You need to use the json module. I'm assuming python2. Try this:
import json
json_data = json.loads('<json_string>')
for i in xrange(len(json_data)):
if(json_data[i]["id"] == "mark"):
del json_data[i]
break
You have a list there with two items, which happen to be dictionaries. To remove the first, you can use list.remove(item) or list.pop(0) or del list[0].
http://docs.python.org/2/tutorial/datastructures.html#more-on-lists

Categories

Resources