<jupyter_start><jupyter_text>Reading/Writing JSON filesJSON 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<jupyter_code>import json<jupyter_output><empty_output><jupyter_text>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.<jupyter_code>input = '''[
  { "id" : "01",
    "status" : "Instructor",
    "name" : "Hrant"
  } ,
  { "id" : "02",
    "status" : "Student",
    "name" : "Jack"
  }
]'''<jupyter_output><empty_output><jupyter_text>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()**.<jupyter_code>data = json.loads(input)<jupyter_output><empty_output><jupyter_text>Let's check the type of the **data** variable.<jupyter_code>type(data)<jupyter_output><empty_output><jupyter_text>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).<jupyter_code>print 'User count:', len(data)<jupyter_output>User count: 2<jupyter_text>Let's print the data to see those 2 elements<jupyter_code>print(data)<jupyter_output>[{u'status': u'Instructor', u'id': u'01', u'name': u'Hrant'}, {u'status': u'Student', u'id': u'02', u'name': u'Jack'}]<jupyter_text>We can even pretty print it, to make the output more user friendly.<jupyter_code>from pprint import pprint
pprint(data)<jupyter_output>[{u'id': u'01', u'name': u'Hrant', u'status': u'Instructor'},
 {u'id': u'02', u'name': u'Jack', u'status': u'Student'}]<jupyter_text>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.<jupyter_code>for item in data:
    print 'Name: ', item['name']
    print 'Id: ', item['id']
    print 'Status: ', item['status'], "\n"<jupyter_output>Name:  Hrant
Id:  01
Status:  Instructor 

Name:  Jack
Id:  02
Status:  Student
{'fraction_non_alphanumeric': 0.10395747194329592, 'fraction_numerical': 0.006792675723567632, 'mean_word_length': 5.2146788990825685, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '98606', 'license': 'Apache-2.0', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1008, 'n_words': 456, 'path': 'Week 3/JSON_part_1.ipynb', 'repo_name': 'HrantDavtyan/Data_Scraping', 'size': '3386'}