0

I've never used JSON before.

I have file that contains 151 differing pre-defined lists. What I want to do is to be able to read the file, and using a class, create 151 objects from the information stored in the file.

The issue I am having is that I can get the file to read, however, it is not creating the objects. From what I can understand, this is because the file contains both integer and string components in the 151 lists - but the read file is only comprised of strings.

Having found JSON - as I understand it - it can serialize and deserialize the information as both integer and string - thus allowing the Class to create the 151 objects.

However - I am having an issue understanding how JSON works exactly, and, not being familiar with it, I am struggling to understand its error messages also.

The Error received is thus:

Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
json.dump(Compile_Index, r"C:\Users\Aphrael\Desktop\Index.py")
File "C:\Python34\lib\json\__init__.py", line 178, in dump
for chunk in iterable:
File "C:\Python34\lib\json\encoder.py", line 429, in _iterencode
o = _default(o)
File "C:\Python34\lib\json\encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <_io.TextIOWrapper name='C:\\Users\\Aphrael\\Desktop\\Index.py' mode='r+' encoding='cp1252'> is not JSON serializable

Would someone be kind enough either to tell me that I am going about what I am attempting to do incorrectly, and set me on the correct path - or otherwise explain what JSON is trying to tell me is the reason that my data is not serializable?

3 Answers3

4

I'm going to take a wild guess since you didn't post your code. You did this:

import json

with open('file.json') as f:
    json.loads(f)

Instead of:

import json

with open('file.json') as f:
    json.loads(f.read())
apex-meme-lord
  • 361
  • 1
  • 8
  • 1
    You have mentioned " ('file.json') ". I am uncertain if this means that my file should be in a .json format, and if so, how to get it into one. Currently the file is a .py document, so I have: `import json with open(r"C:\Users\Aphrael\Desktop\Index.py", "r+") as Compile_Index: Index_Data = Compile_Index.read()` – GoddessAphrael Feb 12 '16 at 06:42
  • sorry for the bad comment format. Still learning the nuances of this website – GoddessAphrael Feb 12 '16 at 06:48
  • The file extension is not relevant. Just call `json.loads(Index_Data)`. – apex-meme-lord Feb 12 '16 at 07:14
  • You should be using `data = json.load(f)` – Christian Sep 05 '22 at 18:17
0

Try to use the code bellow:

json.dumps(objectName.__dict__)

Change objectName for your object.

Joao Luiz Cadore
  • 2,656
  • 1
  • 15
  • 11
0

Thanks for the advice.

I was able to get it to work, however, I was persuaded by my tutor to do the same thing a different way by placing the information into a .dat file and using the exec() function - enabling the code to be easily transferred to other languages, as well as apparantly being safer regarding executing code that may have been tampered with.