FAQ: Learn Python: Files - Reading a JSON File

This community-built FAQ covers the “Reading a JSON File” exercise from the lesson “Learn Python: Files”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise Reading a JSON File

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

2 posts were split to a new topic: Does the print statement need to be indented?

When i save purchase_14781239.json locally and run the code form Jupyter. Why do I get the following error and how do I fix this?

purchase_14781239.json

{
  'user': 'ellen_greg',
  'action': 'purchase',
  'item_id': '14781239',
}
import json

with open('purchase_14781239.json') as purchase_json:
    purchase_data = json.load(purchase_json)

print(purchase_data['user'])
# Prints 'ellen_greg'
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-7-cb612a01aa40> in <module>
      2 
      3 with open('purchase_14781239.json') as purchase_json:
----> 4     purchase_data = json.load(purchase_json)
      5 
      6 print(purchase_data['user'])

~\Anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    297         cls=cls, object_hook=object_hook,
    298         parse_float=parse_float, parse_int=parse_int,
--> 299         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
    300 
    301 

~\Anaconda3\lib\json\__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

~\Anaconda3\lib\json\decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

~\Anaconda3\lib\json\decoder.py in raw_decode(self, s, idx)
    353         """
    354         try:
--> 355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
    357             raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting property name enclosed in double quotes: line 2 column 3 (char 4)

I’m running 3.6.5 |Anaconda custom (64-bit)| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)] on Windows 10

So who does also discover some sort of easter egg and output “Now that's some _serious_ JSON!”? :slightly_smiling_face:

3 Likes

what is difference between creating a dictionary and a json file as i don’t know javascript.

You don’t need to know javascript to work with .json files. It’s just a file type used for long-term storage of an associative array (key-value style). When you’re working with Python you might have temporary variables stored in a dictionary, saving them to a .json file for longer term storage is fairly straightforward.

In the given lessons your message.json file looks like this-
{
“text”: “Now that’s JSON!”,
“secret text”: “Now that’s some serious JSON!”
}
Fairly easy to see how this is just a way of storing data which can be imported easily as a dictionary in Python. A dictionary is Python’s basic associative array data type.

1 Like

so it means we use json instead of a dict because of it’s portability. With json we can store the data outside of the python program and use it whenever needed. right?

Hey!

I found a mistake in this lesson.

At the beginning of the lesson, you give the following JSON as an example:

{
  'user': 'ellen_greg',
  'action': 'purchase',
  'item_id': '14781239',
}

However, valid JSON format does not actually support single quotes, valid JSON only contains double-quotes, so the example needs to be changed to include double-quotes, or else it’s not really valid JSON and will not be parsed properly in some cases in some parsers.