I am using mongoDB with python . I want user to enter a document in the JSON format so that i can insert that into some collection in my db .How can this be done ?
Asked
Active
Viewed 935 times
0
-
Can i do that just by using the command prompt ? I mean the user will enter something like this { name:"joe", age:19,hobby:"table tennis"} and it is taken into a variable . – Meetu Agarwal Aug 23 '15 at 03:34
-
You could. Use `raw_input` in Python 2 or `input` in Python 3, and then run the string through a JSON parser. – Dodo Aug 23 '15 at 05:18
2 Answers
0
As shown in this similar question, Choosing a file in Python with simple Dialog :
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
0
import json
#read string input
dataone= raw_input()
m=json.loads(dataone)
print (m["attribute name in double quotes "])
if the user enters this string {"name":"joe","age":18,"hobby":"singing"} the m["name"] will give the output joe.

Meetu Agarwal
- 59
- 8