import json

"""
Explanation: Reading/Writing JSON files
JSON documents are very popular file format and you may always see them when getting a response from an API. They are very similar to Python dictionaries in a sense that both of them are pairs of keys and values. Because of their popularity, a standing out Python package called JSON is coming preinstalled once you download and install Python. THe package provdes some nice functions for dealing with JSON documents. This notebook provides introduction on reading JSON files from a string variable, local file and from a URL, as well as points the necessary functions to write a variable into an existing or a new JSON file.
Note: Only Parsing JSON from a string variable part is posted now, as the rest was not covered during the lecture.
Parsing JSON from a string variable
End of explanation
"""


input = '''[
  { "id" : "01",
    "status" : "Instructor",
    "name" : "Hrant"
  } ,
  { "id" : "02",
    "status" : "Student",
    "name" : "Jack"
  }
]'''

"""
Explanation: The JSON package has a loads() function which takes a string as an argument and converts it to JSON. THus, let's a create a string which will include a JSON file. In our case this file will be a list of 2 dictionaries, where each of the dictionaires will have 3 keys and a value for each as presented below.
End of explanation
"""


data = json.loads(input)

"""
Explanation: Once we have the string, we can use the abovementioned loads() function from the JSON package to convert the string variable into JSON. Please note, that there is another similar function called load() (without s), which does the same but for local files, rather than a string variable inside Python. Thus, as we have a string variable to load, the correct function will be loads().
End of explanation
"""


type(data)

"""
Explanation: Let's check the type of the data variable.
End of explanation
"""


print 'User count:', len(data)

"""
Explanation: As expected, data is a list, a list that have 2 dictionaire elements.
Let's count how many users we have information on in our list (i.e. we have 2 dictionaires so basically we already know the  answer).
End of explanation
"""


print(data)

"""
Explanation: Let's print the data to see those 2 elements
End of explanation
"""


from pprint import pprint

pprint(data)

"""
Explanation: We can even pretty print it, to make the output more user friendly.
End of explanation
"""


for item in data:
    print 'Name: ', item['name']
    print 'Id: ', item['id']
    print 'Status: ', item['status'], "\n"

"""
Explanation: Fine, we see the two elements. Now, let's print the content (values) by choosing them by keys. For that purpose, we must create a for loop, that will iterate over the elements in the list (2 dictionaires, in our case) and for each of the elements print some text (e.g. the key, which are "Name", "ID" and "Status") followed by the value of that key.
End of explanation
"""


