I am trying to read the "transcript" from my json file and i just can't get inside it. I can access the main JSON but not the specific part of it.
My JSON file
"results": [ {
"alternatives": [ {
"confidence": 0.90822744,
"transcript":"bunch of words" ,
"words": [ {
"endTime": "1.400s",
"startTime": "0s",
"word": "bunch"
},...
My Code snippet
f = open('C:\\Users\\Kerem\\Desktop\\YeniGoogleapi\\MugeAnliileTatliSert7Kasim.json', encoding="utf8")
data = json.load(f)
for i in data['results']:
print(i)
Related
I'm new to python. I'm running python on Azure data bricks. I have a .json file. I'm putting the important fields of the json file here
{
"school": [
{
"schoolid": "mr1",
"board": "cbse",
"principal": "akseal",
"schoolName": "dps",
"schoolCategory": "UNKNOWN",
"schoolType": "UNKNOWN",
"city": "mumbai",
"sixhour": true,
"weighting": 3,
"paymentMethods": [
"cash",
"cheque"
],
"contactDetails": [
{
"name": "picsa",
"type": "studentactivities",
"information": [
{
"type": "PHONE",
"detail": "+917597980"
}
]
}
],
"addressLocations": [
{
"locationType": "School",
"address": {
"countryCode": "IN",
"city": "Mumbai",
"zipCode": "400061",
"street": "Madh",
"buildingNumber": "80"
},
"Location": {
"latitude": 49.313885,
"longitude": 72.877426
},
I need to create a data frame with schoolName as one column & latitude & longitude are others two columns. Can you please suggest me how to do that?
you can use the method json.load(), here's an example:
import json
with open('path_to_file/file.json') as f:
data = json.load(f)
print(data)
use this
import json # built-in
with open("filename.json", 'r') as jsonFile:
Data = jsonFile.load()
Data is now a dictionary of the contents exp.
for i in Data:
# loops through keys
print(Data[i]) # prints the value
For more on JSON:
https://docs.python.org/3/library/json.html
and python dictionaries:
https://www.programiz.com/python-programming/dictionary#:~:text=Python%20dictionary%20is%20an%20unordered,when%20the%20key%20is%20known.
I'm new to json file syntax and I was wondering how to delete "phones" and its values from my json file so that end, start, alignedWord, and word are my only values in the words key.
My Code:
with open('dest_file.json', 'w') as dest_file:
with open('test1.json', 'r') as j:
contents = json.loads(j.read())
for value in contents['words']:
del(value['case'])
del(value['endOffset'])
del(value['startOffset'])
dest_file.write(json.dumps(contents,indent = 4))
Example JSON object:
"words": [
{
"alignedWord": "the",
"end": 6.31,
"phones": [
{
"duration": 0.09,
"phone": "dh_B"
},
{
"duration": 0.05,
"phone": "iy_E"
}
],
"start": 6.17,
"word": "The"
},
In addition to this, what exactly datatype is phones exactly and why was I not able to delete it via my current code?
"phones" in above case will be a list of dictionaries. Also the "words" in itself is list of dictionaries itself. You need to access items in list first and then can jump on to dictionary pops as suggested above.
words =[
{
"alignedWord": "the",
"end": 6.31,
"phones": [
{
"duration": 0.09,
"phone": "dh_B"
},
{
"duration": 0.05,
"phone": "iy_E"
}
],
"start": 6.17,
"word": "The"
}
]
to display the single element:
words[0]['phones']
to remove:
words[0].pop('phones')
you can use above code to iterate and pop as suggested!
Try this:
If you want to save the edited json to a new file:
import json
with open('dest_file.json', 'w') as dest_file:
with open('source_file.json', 'r') as source_file:
for line in source_file:
element = json.loads(line.strip())
if 'phones' in element:
del element['phones']
dest_file.write(json.dumps(element))
If you want to re-write to the same json after editing:
import json
with open('dest_file.json') as data_file:
data = json.load(data_file)
for element in data:
if 'phones' in element:
del element['phones']
with open('dest_file.json', 'w') as data_file:
data = json.dump(data, data_file)
Inside a JSON elements, you can manipulate data either like an array / list, or as a dict. In this case, you can do value.pop("phones").
e.g.
import json
data = '''
[
{
"alignedWord": "the",
"end": 6.31,
"phones": [
{
"duration": 0.09,
"phone": "dh_B"
},
{
"duration": 0.05,
"phone": "iy_E"
}
],
"start": 6.17,
"word": "The"
}]'''
contents = json.loads(data)
for value in contents:
value.pop('phones')
print(json.dumps(contents))
See it running: https://repl.it/repls/DarkgreyLightcoralInstitution
I have a JSON file which has some key-value pairs in Arrays. I need to update/replace the value for key id with a value stored in a variable called Var1
The problem is that when I run my python code, it adds the new key-value pair in outside the inner array instead of replacing:
PYTHON SCRIPT:
import json
import sys
var1=abcdefghi
with open('C:\\Projects\\scripts\\input.json', 'r+') as f:
json_data = json.load(f)
json_data['id'] = var1
f.seek(0)
f.write(json.dumps(json_data))
f.truncate()
INPUT JSON:
{
"channel": "AT",
"username": "Maintenance",
"attachments": [
{
"fallback":"[Urgent]:",
"pretext":"[Urgent]:",
"color":"#D04000",
"fields":[
{
"title":"SERVERS:",
"id":"popeye",
"short":false
}
]
}
]
}
OUTPUT:
{
"username": "Maintenance",
"attachments": [
{
"color": "#D04000",
"pretext": "[Urgent]:",
"fallback": "[Urgent]:",
"fields": [
{
"short": false,
"id": "popeye",
"title": "SERVERS:"
}
]
}
],
"channel": "AT",
"id": "abcdefghi"
}
Below will update the id inside fields :
json_data['attachments'][0]['fields'][0]['id'] = var1
Here is the sample JSON which I am trying to parse in python.
I am having hard time parsing through "files":
Any help appreciated.
{
"startDate": "2016-02-19T08:19:30.764-0700",
"endDate": "2016-02-19T08:20:19.058-07:00",
"files": [
{
"createdOn": "2017-02-19T08:20:19.391-0700",
"contentType": "text/plain",
"type": "jpeg",
"id": "Photoshop",
"fileUri": "output.log"
}
],
"status": "OK",
"linkToken": "-3418849688029673541",
"subscriberViewers": [
"null"
]
}
To print the id of each file in the array:
import json
data = json.loads(rawdata)
files = data['files']
for f in files:
print(f['id'])
I want to store data in a .json file as using python language-
{object:
{
name: "abcd",
id: "fwfwfwf"
}
{
name: "efgh",
id: "wderds"
}
..
.. and so on
}
{container:
{
name: "pros",
id: "zxcdsef"
}
{
name: "mnop",
id: "waqsred"
}
..
.. and so on
}
Again now I want to read particular member of object/container in the file using f.read() or similar methods.\
How can I parse this file using python and JSON ??
#
Sir One more thing I want to ask. Suppose my data.json file is like -
{
"object": [
{
"name": "abcd",
"id": "fwfwfwf"
},
{
"name": "efgh",
"id": "wderds"
}
]
}
{
"container": [
{
"name": "pqrs",
"id": "fwfwfwf"
},
{
"name": "mnop",
"id": "wderds"
}
]
}
Now I want to add one more container in this file which will go under Containers.
Can you please tell me how will I write new container in the file using f.write()
first create valid json. You can validate your json using any link like
{
"object": [
{
"name": "abcd",
"id": "fwfwfwf"
},
{
"name": "efgh",
"id": "wderds"
}
]
}
Now python script
import json
with open('data.json') as data_file:
data = json.load(data_file)
And you can access data like:
data["objects"][0]["id"] # will return 'fwfwfwf'
data["objects"][1]["name"] #will return 'efgh'
use http://jsonlint.com/ to validate your json. In json files identifiers have double quotes. numbers int/float need not have.
In general a json file is a python dictionary in text format. You can use the json module to parse json files.
import json
myfile = open('yourfilename', 'r')
myjsondata_as_dict = json.load(myfile)
after this myjsondata_as_dict will have your json data as a dictionary, provided there were no errors in json format.
This is the syntax for dumping data into JSON prettily.
with open('abc.json', 'wb') as outfile:
json.dump(obj, outfile, indent = 4)
For loading from JSON file, use
with open('abc.json') as infile:
data = json.load(infile)
More info http://freepythontips.wordpress.com/2013/08/08/storing-and-loading-data-with-json/