
The json.load method is passed the file handle and reads the complete file. At which time the file will automatically be closed. The with statement is in effect until we un-indent. Notice the : at the end of the line and the subsequent indentation.
We have chosen to use the with statement to open the SAFI.json file. We import the json package with an import statement. Clicking on the first tab, “viewer” also gives another view of the data. Try pasting or loading JSON data in the second tab called “text” on that website and then clicking on the “format” button. There are many websites which allow viewing or beautifying JSON data with ease. JSON files can be opened using text editors e.g., notepad, nano, textedit, etc. Browser extensions are often available for ease of viewing JSON data e.g., Chrome, Firefox, Safari, etc. JSON can often be viewed directly in the web browsers: example. Can be more difficult to process and display than csv formatted data. It is more verbose than the equivalent data in csv format. Document structures can be complex and nested. Documents do not all have to have the same structure within the same file. The equivalent of the column name and column values are in every record. Each record (or document as they are called) is self contained. Very popular data format for APIs (e.g. The questions themselves have been replaced with unique Keys, the values are the answers.īecause detailed surveys are by nature nested structures making it possible to record different levels of detail or selectively ask a set of specific questions based on the answer given a previous question, the structure of the answers for the survey can not only be complex and convoluted, it could easily be different from one survey respondent’s set of answers to another. VISUAL JSON PYTHON SERIES
The JSON represents the answers to a series of survey questions.
This is the output file from an electronic survey system called ODK. The JSON file we are going to use is the SAFI.json file. We are going to read a file of data formatted as JSON, convert it into a dictionary object in Python then selectively extract Key-Value pairs and create a csv file from the extracted data. The JSON format although somewhat verbose is not only Human readable but it can also be mapped very easily to a Python dictionary object. JSON is now the preferred data format used by APIs (Application Programming Interfaces). The JSON data format was designed as a way of allowing different machines or processes within machines to communicate with each other by sending messages constructed in a well defined format. In practice you will not be doing this manually, instead like most data you will read it in from a file. The ability to create dictionaries containing lists and other dictionaries, makes the dictionary object very versatile, you can create an arbitrarily complex data structure of dictionaries within dictionaries. PersonDict = print ( personDict ) for child in personDict : print ( child )