Writing to a specific dictionary inside a json file Python - python

I have a .json file with some information
{"items": [{"phone": "testp"}, {"phone": "Test2"}]}
I want to add another dictionary into the next available slot in the array with some more information. I have tried many ways however no of them work, has anyone got any ideas?

dictionary["items"].append(new_dictionary)
You access key "items" in your dictionary, which is a list of dictionaries, to that list you simply append your new dictionary

You can read the json into a variable using load function.
And then append the value of array using
myVar["items"].append(dictVar)

Related

How Best to Convert a Pickled Python Dictionary Using Python shelve Library (bsddb) to a JSON File that has tuple Values for Dictionary Keys

I have an application where I need to convert Python shelve pickled dictionary files to JSON files.
import ujson, shelve
with open("shelveFile", "r") as sfile:
shelve_dict=shelve.open(sfile)
py_dict= dict(shelve_dict)
with open("jsonfile.json","w") as jsonfile:
ujson.dump(py_dict, jsonfile)
with open("jsonfile.json",'r') as readJSONfile:
ujson.loads(readJSONfile.read())
Note: If I use ujson.load(jsonfile2) I get a serialization error.
The issue I have: the shelve file uses Python tuple datatypes for some of the dictionary keys. I am able to use ujson.dump to save as JSON but when I try to use ujson.load(jsonfile) the keys get loaded as strings and not tuples.
Short of using a dictionary comprehension to convert the keys(unsure of that exact syntax), is there a library that would convert a shelve file to a JSON file that I could load back into a Python dictionary object?
When loaded by ujson.loads(fileobj.read()) method:
{"('tuplekey1','tuplekey2')": value,}
Should be:
{('tuplekey1','tuplekey2'): value,}
(please do not down-vote if question is not clear, I will do my best to clarify if needed... I do not post questions here often.)
Tuples cannot be used as dictionary keys when you want to save data with shelve or json. So in your case string representation of tuples was used instead of actual tuples - repr(('tuplekey1', 'tuplekey2')) gives "('tuplekey1', 'tuplekey2')".
So you need additional step to parse strings into tuples. For example, eval("('tuplekey1', 'tuplekey2')") or tuple(val.strip("()' ") for val in a.split(',')). Of course, you need to know (or recognize) what strings should be parsed into tuples.
The problem may be avoided - data should stored in another manner - another representation of tuples or another structure of data.

Using nested for loops to iterate through JSON file of tweets in Python

So I am new to Python, but I know what I am trying to accomplish. Basically, I have the output of tweets from twitter in a JSON file loaded into Python. What I need to do is iterate through the tweets to access the "text" key, that has the text of each tweet, because that's what I'm going to use to do topic modeling. So, I have discovered that "text" is triple nested in this data structure, and it's been really difficult to find the correct way to write the for loop code in order to iterate through the dataset and pull the "text" from every tweet.
Here is a look at what the JSON structure is like: https://pastebin.com/fUH5MTMx
So, I have figured out that the "text" key that I want is within [hits][hits][_source]. What I can't figure out is the appropriate for loop to iterate through _source and pull those texts. Here is my code so far (again I'm very beginning sorry if try code is way off):
for hits in tweets["hits"]["hits"]:
for _source in hits:
for text in _source:
for item in text:
print(item)
also tried this:
for item in tweets['hits']["hits"]["_source"]:
print(item['text'])
But I keep getting either syntax errors for the first one then "TypeError: list indices must be integers or slices, not str" for the second one. I am understanding that I need to specify some way that I am trying to access this list, and that I'm missing something in order to show that its a list and I am not looking for integers as an output from iterations...(I am using the JSON module in Python for this, an using a Mac with Python3 in Spyder)
Any insight would be greatly appreciated! This multiple nesting is confusing me a lot.
['hits']["hits"] is not dictionary with ["_source"]
but a list with one or many items which have ["_source"]
it means
tweets['hits']["hits"][0]["_source"]
tweets['hits']["hits"][1]["_source"]
tweets['hits']["hits"][2]["_source"]
So this should work
for item in tweets['hits']["hits"]:
print(item["_source"]['text'])
Not sure if you realize it, but JSON is transformed into a Python dictionary, not a list. Anyway, let's get into this nest.
tweets['hits'] will give you another dict.
tweets['hits']['hits'] will give you a list (notice the brackets)
This apparently is a list of dictionaries, and in this case (not sure if it will always be), the dict with the "_source" key you are looking for is the first one,so:
tweets['hits']['hits'][0] will give you the dict you want. Then, finally:
tweets['hits']['hits'][0]['_source'] should give you the text.
The value of the second "hits" is a list.
Try:
for hit in tweets["hits"]["hits"]:
print(hit["_source"]["text"])

indexing nested dictionary elements

I am trying to access the dictionary elements in python, which looks like this:
mydict= {'message':'','result':[{'abc':1,'xyz':2}]}
I want to access the value of key 'xyz'.Is there any direct method to access it in python.
Any help is greatly appreciated.
In steps,
mydict['result'] returns [{'abc':1,'xyz':2}]
mydict['result'][0] returns {'abc':1,'xyz':2}
mydict['result'][0]['xyz'] returns 2
The answer above is wrong in that mydict['result'][0] doesn't work because you don't index dictionaries like a list.
To access 1, you must use the actual string in the key of the nested dictionary: mydict['result']['abc']

How to access the list inside dictionary in python?

I have dict structure as shown in image.I am want to access wavelength list values of it.
How to access it or store the wavelength list in another variable.
This should simply be:
dict['wavelength']
Where "dict" is your dictionary name; which looks like it's "n2" maybe. The returned value will be the list that you're looking for.

Copying a list one collection to another collection

i have a document in collection coll1 in this form:
{_id: 1, "value" : {"listOfNumbers" : [1,2,3]}}
I would like to know, how can I copy this list into an existing list of collection coll2 using pymongo.
I found this query which will replace the existing list of coll2 with the list [3,2,1]:
db.coll2.update({_id:1}, {$set: {'value.listOfNumbers' : [3,2,1]}})
The problem is, I don't know how to get the list of coll1.
Also, what would be the easiest way to check if the two lists are the same?
I thank you in advance for your replies and your effort to help.
coll1 for me seems to be a simple dictionary, where it should be easy to get the value of the keyword 'value' via
coll1['value']
As the entry is another dictionary, you should be able to get the list via
coll1['value']['list of numbers']
For comparison, it depends on the fact, that lists are only equal in python, if the order and the value of the elements is equal. That should be easy to check with isequal (==).

Categories