This question already has answers here:
Nested dictionary value from key path
(9 answers)
Closed 1 year ago.
I want to know how to read a specific objects value from within a JSON object.
"worker_01": {
"data": {
"name": "Juan",
"manager": 0,
"weight": 75,
"positions": {
"FOH": 1,
"MOH": 1,
"BOH": 1
}
}
},
I know previously in Node.js I could read them by doing *.get(worker_01.data.name)*
but python doesn't really allow that. I want to know how do I do something similar to that in python.
Here is my source code.
import json as js
data_json = open('hello.json')
data_worker = js.load(data_json)
for i in data_worker['worker_01']:
print(i)
In Python, you can read JSON as a big python dictionary with mini listed dictionaries in between them all. With that in mind, you can index specific attributes from the JSON. For example based on your JSON, if a user wanted to get data on a persons name, you would do data_worker['worker_01']['data']['name'] to get the output 'Juan'
Here is more resources for you to look at too!
https://www.w3schools.com/python/python_json.asp
https://www.codegrepper.com/code-examples/python/how+to+get+specific+data+from+json+using+python
https://www.w3schools.com/python/python_dictionaries.asp
Related
This question already has answers here:
What is the difference between Python's list methods append and extend?
(20 answers)
How do I write JSON data to a file?
(16 answers)
Closed 4 days ago.
The community reviewed whether to reopen this question 4 days ago and left it closed:
Original close reason(s) were not resolved
I am working with an API that returns the following format:
{
"count": 900,
"next": "api/?data&page=2",
"previous": null,
"results":
[{json object 1}, {json object 2}, {...}]
}
Problem is that I want to retrieve all "results" from all pages, and save that into one json file.
I'm thinking of a while loop that keeps making requests to the API and aggregating the resulting "results" into one variable, until the "next" value is null.
Something like
while json1["next"] != null:
r = request.get(apiURL, verify=False, allow_redirects=True, headers=headers, timeout=10)
raw_data = r.json()["results"]
final_data.update(raw_data)
I tried it but since r.json()["results"] is a list I don't know how to handle the different formats and transform that into a JSON file
When trying to do final_data.update(raw_data) it gives me an error saying:
'list' object has no attribute 'update'
Or when trying json.loads(raw_data) it gives me:
TypeError: the JSON object must be str, bytes, or bytearray, not list"
JSON file is a text file. To save your raw_data, which is a list, in a text file, you need to encode it using json.dumps():
import json
with open('output.json', 'w', encoding="utf-8") as f:
raw_data_as_string = json.dumps(raw_data)
f.write(raw_data_as_string)
To aggregate the results from different pages, your final_data can be a list, created before you iterate the pages, and then you can final_data.extend(raw_data) in a loop, where raw_data contains results from a single page.
After that you json.dumps(final_data) as shown earlier.
I am trying to add data to the Firestore database without overwriting it. The data is in the format written below and has numerous other "Question" in the same format and I want to add this to just one document.
{
"Question": String,
"Answer": String,
}
The same question has been asked here but it covers it in java and not in python. I have tried updating it and setting it but it has only been overwriting it.
Note that all of my Questions are elements in a list in this format:
['{\n "Question": String,\n "Answer":String \n}, ...]
What I am currently doing in my code is going through the array and performing the code below:
doc_ref = db.collection(u"Questions").document(u"ques")
doc_ref.update(questionsAnswers)
but this only leaves me with the last question added to the database.
Use the update method to change the contents of an existing document as shown in the documentation.
city_ref = db.collection(u'your-collection').document(u'your-document')
city_ref.update({u'your-field': u'your-field-value'})
I suggest also using the API documentation.
This question already has answers here:
How to get key names from JSON using jq
(9 answers)
Closed 4 years ago.
I would like to get first objects (don't know if it's the right name) of my json file that is huge (more than 120k lines), so I can't parse it manually.
Format is like this :
"datanode": [
{
"isWhitelisted": true,
"metricname": "write_time",
"seriesStartTime": 1542037566944,
"supportsAggregation": true
},
{
"isWhitelisted": true,
"metricname": "dfs.datanode.CacheReportsNumOps",
"seriesStartTime": 1542037501137,
"supportsAggregation": true,
"type": "COUNTER"
},
{
"isWhitelisted": true,
"metricname": "FSDatasetState.org.apache.hadoop.hdfs.server.datanode.fsdataset.impl.FsDatasetImpl.EstimatedCapacityLostTotal",
"seriesStartTime": 1542037495521,
"supportsAggregation": true,
"type": "GAUGE"
},
],
"toto": [
....
And what I need is to extract this : datanode, toto, etc. Only the name.
Can you help me please ?
I tried using jq without success.
You can use jq's keys functionality
jq 'keys' file.json
In the future try to improve on which words you use to describe the different parts the json data. You asked about objects in the text, but actually refer to the keys.
A more fitting title for the question would have been: "How to get all top level keys of json data using jq?" And with this, more correct, wording you find already answered questions like this one: How to get key names from JSON using jq
Also provide a complete and valid example structure and the expected result like this:
{
"one_key": {
"foo": "bar"
},
"another_one": {
"bla": "bla"
}
}
And desired result:
[
"another_one",
"one_key"
]
I have a json whose first few lines are:
{
"type": "Topology",
"objects": {
"counties": {
"type": "GeometryCollection",
"bbox": [-179.1473399999999, 17.67439566600018, 179.7784800000003, 71.38921046500008],
"geometries": [{
"type": "MultiPolygon",
"id": 53073,
"arcs": [
[
[0, 1, 2]
]
]
},
I built a python dictionary from that data as follows:
import json
with open('us.json') as f:
data = json.load(f)
It's a very long json (each county in the US). Yet when I run: len(data) it returns 4. I was a bit confused by that. So I set out to probe further and explore the data:
data['id']
data['geometry']
both of which return key errors. Yet I know that this json file is defined for those properties. In fact, that's all the json is, its the id for each county 'id' and a series of polygon coordinates for each county 'geometry'. Entering data does indeed return the whole json, and I can see the properties that way, but that doesn't help much.
My ultimate aim is to add a property to the json file, somewhat similar to this:
Add element to a json in python
The difference is I'm adding a property that is from a tsv. If you'd like all the details you may find my json and tsv here:
https://gist.github.com/diggetybo/ca9d3c2fed76ddc7185cf966a65b8718
For clarity, let me summarize what I'm asking:
My question is: Why can't I access the properties in the above way? Can someone provide a way to access the properties I'm interested in ('id','geometries') Or better yet, demonstrate how to add a property?
Thank you
json.load
Deserialize fp (a .read()-supporting file-like object containing a
JSON document) to a Python object using this conversion table.
[] are for lists and {} are for dictionaries.So this is an example to get id:
with open("us.json") as f:
c=json.load(f)
for i in c["objects"]["counties"]["geometries"]:
print i["id"]
And the structure of your data is like this:
{
"type":"xx",
"objects":"xx",
"arcs":"xx",
"transform":"xx"
}
So the length of data is 4.You can append data or add a new element just like using list and dict.See more details from Json.
Hope this helps.
This is the way reading from a .json file on ubuntu terminal:
python -c "import json;print json.loads(open('json_file.json', 'r').read())['foo']['bar']"
What I'd like to do is altering the JSON file, adding new objects and arrays. So how to do this in python?
json_file.json:
{
"data1" :
[
{
"unit" : "Unit_1",
"value" : "20"
},
{
"unit" : "Unit_2",
"value" : "10"
}
]
}
First of all, create a new python file.
import json
data = json.loads(open('json_file.json', 'r').read())
The data is then just a bunch of nested dictionaries and lists.
You can modify it the same way you would modify any python dictionary and list; it shouldn't be hard to find a resource on this as it is one of the most basic python functionalities. You can find a complete reference at the official python documentation, and if you are familiar with arrays/lists and associative arrays/hashes in any language, this should be enough to get you going. If it's not, you can probably find a tutorial and if that doesn't help, if you are able to create a well-formed specific question then you could ask it here.
once you are done, you can put everything back into json:
print json.dumps(data)
For more information on how to customize the output, and about the json module overall, see the documentation.