0

I know there are quite a few json/python questions out there but I can't seem to figure this one out. I am trying to serialize two lists into the same file. In order to do that I create a new class that holds the two lists:

class newJSON(object):
    def __init__(self, list1, list2):
        self.data = {'data': list1, 'info' : list2}

I need the resulting data file to look like the following:

{
    "data" : [
        {
            "name" : "aName" ,
            "coordinates" : {"obj2" : 33, "obj3" : 71} 
        } , {
            "name" : "bName" ,
            "coordinates" : {"obj2" : 12, "obj3" : 77} 
        }
        ] ,
    "info" : [
        {
            "first" : ["xxx" , "yyy"] ,
            "space" : 21
        } , {
            "first" : ["aaa" , "bbb"] ,
            "space" : 12
        }
        ]
}

So then I go to decode the object as recommended in Serializing python object instance to JSON and several others:

jsonToEncode = newJSON(myList1, myList2)
myNewJSONData = json.dumps(jsonToEncode.__dict__)

However I get the "is not JSON serializable error"... I have tried this with and without the dict but to no success. The JSON must be in the format shown above. What is the problem?

Thanks

****EDIT****

in order to make the two lists, I take a json file which is formatted exactly like the json shown and do the following:

list1 = [obj1(**myObj) for myObj in data["data"]]

and the same for list2. obj1 is made like this:

class obj1(object):
    def__init__(self, name, coordinates): 
        self.name = name 
        self.coordinate = coordinates
Community
  • 1
  • 1
thebiglebowski11
  • 1,365
  • 10
  • 38
  • 72

4 Answers4

2

There is no need to create a new object. Simply serialize the dictionary directly:

myNewJSONData = json.dumps({'data': list1, 'info': list2})

However, your code should have worked otherwise. You probably have data contained in list1 and list2 that is not serializable.

Martijn Pieters
  • 889,049
  • 245
  • 3,507
  • 2,997
  • The data is represented exactly as shown, which part would be unserializable? – thebiglebowski11 Feb 28 '13 at 13:46
  • @Ranlou In the code you posted you did not show how `list1` and `list2` are created. What Martijn is saying is that the problem is in the content of `list1` and `list2` not the rest of the code. – Bakuriu Feb 28 '13 at 13:50
  • @Ranlou: I cannot see what else you may have added to the `newJSON` class, for example. But there is *no need* for that custom class, to get the output you want, my code more than suffices. If you still get the same `is not JSON serializable error` error then please show us what `list1` and `list2` *are*. – Martijn Pieters Feb 28 '13 at 13:51
  • in order to make the two lists, I take a json file which is formatted exactly like the json shown and do the following: list1 = [obj1(**myObj) for myObj in data["data"] and the same for list2. obj1 is made like this: def__init__(self, name, coordinates): self.name = name self.coordinate = coordinates I really don't see how this would be a problem... – thebiglebowski11 Feb 28 '13 at 13:58
  • @Ranlou: That is a *huge* difference. JSON does not serialize custom classes, it serializes primitive Python types instead. Please do update your question with that code. – Martijn Pieters Feb 28 '13 at 14:00
  • Is there a solution to the problem then? – thebiglebowski11 Feb 28 '13 at 14:02
  • The code is shown above, I literally do nothing to it after it is put into the two list objects – thebiglebowski11 Feb 28 '13 at 14:14
  • @Ranlou: *Why* are you using those classes? What is your use case? You cannot serialize those without extra work extracting the data again. – Martijn Pieters Feb 28 '13 at 14:15
  • In some cases, I will query on the object to find certain values – thebiglebowski11 Feb 28 '13 at 14:22
  • @Ranlou: And using item access is not enough? Your class doesn't add any extra functionality, at least not in what you shared. – Martijn Pieters Feb 28 '13 at 14:22
  • I actually have it working... the only problem now is that the json that it outputs is a string (i.e. it leads and ends with a " ), not a json object – thebiglebowski11 Feb 28 '13 at 14:35
0

Try myNewJSONData = json.dumps(jsonToEncode.data) instead.

Or even myNewJSONData = json.dumps({'data': list1, 'info' : list2}). Why do you use that class jsonToEncode anyway?

Alfe
  • 47,603
  • 17
  • 84
  • 139
0

try this:

myNewJSONData = json.dumps(jsonToEncode.data, indent=2)

or this:

>>> class newJSON2(dict):
...     def __init__(self, list1, list2):
...         self['data'] = list1
...         self['info'] = list2
>>> 
>>> 
>>> json2 = newJSON2(list1, list2)
>>> json2
{'info': [{'space': 21, 'first': ['xxx', 'yyy']}, {'space': 12, 'first': ['aaa', 'bbb']}], 'data': [{'name': 'aName', 'coordinates': {'obj3': 71, 'obj2': 33}}, {'name': 'bName', 'coordinates': {'obj3': 77, 'obj2': 12}}]}
>>> print json.dumps(json2, indent=2)
{
  "info": [
    {
      "space": 21, 
      "first": [
        "xxx", 
        "yyy"
      ]
    }, 
    {
      "space": 12, 
      "first": [
        "aaa", 
        "bbb"
      ]
    }
  ], 
  "data": [
    {
      "name": "aName", 
      "coordinates": {
        "obj3": 71, 
        "obj2": 33
      }
    }, 
    {
      "name": "bName", 
      "coordinates": {
        "obj3": 77, 
        "obj2": 12
      }
    }
  ]
}
>>> 
Zokis
  • 415
  • 6
  • 12
  • I still get the not serializable error, what could make this data not serializable? – thebiglebowski11 Feb 28 '13 at 13:54
  • the json accepts only certain types (you can see here http://json.org/) for any others you'll have to define what we do same as example in http://docs.python.org/2/library/json.html#json.JSONEncoder – Zokis Mar 01 '13 at 13:46
0

Use simplejson library.

from simplejson import loads, dumps
print loads(json_string) # Converts a JSON string to dict
print dumps(python_object) # Converts any valid python object/dict to valid JSON string

Take a look at https://pypi.python.org/pypi/simplejson.

Garfield
  • 2,257
  • 3
  • 25
  • 51
  • From the link you posted: *simplejson is the externally maintained development version of the json library included with Python 2.6 and Python 3.0, but maintains backwards compatibility with Python 2.5* So, you don't have to install it and can simply use the `json` module that comes with python itself. The external package is more up-to-date, but nothing else. – Bakuriu Feb 28 '13 at 13:52