19
loading...
This website collects cookies to deliver better user experience
{
"name": "Luke Skywalker",
"height": 172,
"mass": 77,
"hair_color": "blond",
"birth_year": "19BBY",
"gender": "male",
"homeworld": "Tatooine",
"films": [
"A New Hope",
"Return of the Jedi",
"The Empire Strikes Back",
"Revenge of the Sith"
],
"jedi_knight": true
}
"films"
to be an array of objects, with each object containing key-value pairs with more data about each film (and I could continue on like this, having objects and arrays nested within objects and arrays)."films": [
{
"title": "A New Hope",
"year": "1977"
},
{
"title": "Return of the Jedi",
"year": "1983"
},
{
"title": "The Empire Strikes Back",
"year": "1980"
},
{
"title": "Revenge of the Sith",
"year": "2005"
},
],
datetime
module to get the current date, and then use its method isoformat()
to convert it to ISO format. Then use json.dumps()
to convert the date to JSON.import datetime
datetime = datetime.datetime.now()
formatted_datetime = datetime.isoformat()
json_datetime = json.dumps(formatted_datetime)
.json
extension, and the official MIME type for JSON is "application/json", which is what we would use as the content-type in the headers object of a fetch request.JSON.stringify()
to convert Javascript to JSON, and use JSON.parse()
to convert JSON to JavaScript:const jedi = {
name: 'Luke Skywalker',
mass: 77,
homeWorld: 'Tatooine',
}
const jediString = JSON.stringify(jedi)
console.log(jediString)
//JSON string "{"name":"Luke Skywalker","mass":77,"homeWorld":"Tatooine"}"
console.log(JSON.parse(jediString))
// Javascript object {name:"Luke Skywalker",mass:77,homeWorld:"Tatooine"}
dict
to JSON, you can import the built-in module json, and then use the method json.dumps()
on the dict
. And to convert JSON to a Python dict
, use the method json.loads()
:import json
# a Python object (dict):
jedi = {
"name": "Luke Skywalker",
"mass": 77,
"home_world": "Tatooine"
}
jedi_string = json.dumps(jedi)
print(jedi_string)
# JSON string {"name": "Luke Skywalker", "mass": 77, "home_world": "Tatooine"}
print(json.loads(jedi_string))
# Python dict {'name': 'Luke Skywalker', 'mass': 77, 'home_world': 'Tatooine'}
{
"metadata": {
"transaction_key": "lrCXFhkJPoTZ6Ezh9G24WabGcR5vMI/ksuSVtt1abe6abrr2+mGZb4CDTFGLedIxYUsI5MYvAEmDagh6AMEBFEyvC0qIF3YR5A31UMZkE4USmjWQSYyIukZxMtH9918TBLtUOvyeuTVeOcwdLUODqRA3uP67tF19eEKSza6Yj+IiQtib7yeHJWn5YzXPwX/5FOOQupKJoHz6dUH5lwjdhi9ykG6Nn87GDuZBzsejpEGsKJbzIgOQPJUrJTec09MDO95Bw9lj2cMPw1R/ZqBYbMtGvTamhopVl8XxV9Sg5blZkf8bs2KcRilYypQOvXggDGHLPxGNChBDFrvcR9Qi+eLLnEzPrHTsc6FjsFl/YgQ+Cw30RmpFiJceUXM2ed3/ojE5GLzsfSBeost4",
"request_id": "eeaa1992-5729-4f2c-a73f-6224d78a47b8",
"sha256": "8d2b4b8cc76cd35a5f9bde55ce92de211216849cca1407b1ad0d5d4d6ed610a2",
"created": "2021-11-16T19:55:40.059Z",
"duration": 24.696,
"channels": 1,
"models": [ "41757536-6114-494d-83fd-c2694524d80b" ]
},
"results": {
"channels": [
{
"alternatives": [
{
"transcript": "This is the weapon of a jedi night, not as clumsy or random as a blast an elegant weapon. For all civilized day. Over a thousand generations, the Jedi knights the guardians of peace of justice in the old republic before the dark times before they can pass.",
"confidence": 0.90745026,
"words": [
{
"word": "this",
"start": 0.65999997,
"end": 0.78,
"confidence": 0.9960715,
"speaker": 0,
"punctuated_word": "This"
},
...
]
}
]
}
],
"utterances": [
{
"start": 0.65999997,
"end": 2.56,
"confidence": 0.8840211,
"channel": 0,
"transcript": "This is the weapon of a jedi night,",
"words": [
{
"word": "this",
"start": 0.65999997,
"end": 0.78,
"confidence": 0.9960715,
"speaker": 0,
"punctuated_word": "This"
}
...
],
"speaker": 0,
"id": "791ad5c3-b097-4ab3-b26f-5c0c8595c0e5"
}
]
}
}
transcript
data is.) But this response is giving me metadata and a whole bunch of other data, including individual words and data about those words! Really nice, but a little more than we need at the moment.transcript
string. The way to do this is to assign the response from Deepgram to a variable called response (or whatever you want to call it), and then connect the keys and/or indices following this path:key -> key -> index0 -> index0 -> key
[obj] [obj] [arr] [arr] [obj]
response.results.channels[0].alternatives[0].transcript
response['results']['channels'][0]['alternatives'][0]['transcript']
This is the weapon of a jedi night, not as clumsy or random as a blaster, an elegant weapon. For more civilized day. Over a thousand generations, the Jedi knights the guardians of peace of justice in the old republic before the dark times.