ziggy
ziggy

Reputation: 1538

Cannot serialize dictionary to JSON

I have this JSON

import json
f={'file_id': '2019_08_02_12_05_30.893834.csv', 'file_rows': [[b'Borough,OnStreet,CrossStreetOne,CrossStreetTwo,Asset\r\n'], [b'Brooklyn,Hoyt Street,Schermerhorn Street,Livingston Street,CityBench\r\n'], [b'Manhattan,HUDSON ST,BROOME ST,DOMINICK ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,CHARLTON ST,KING ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,CHARLTON ST,KING ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,KING ST,W HOUSTON ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,SPRING ST,VAN DAM ST,Bike Rack\r\n'], [b'Bronx,CROSS BRONX EP,HUGH J GRANT CI,VIRGINIA AV,Bike Rack\r\n'], [b'Brooklyn,JACKSON ST,HUMBOLDT ST,WOODPOINT RD,Bike Rack\r\n']]}

print(json.dumps(f.decode('utf-8')))

gives me this error

print(json.dumps(f.decode('utf-8')))
AttributeError: 'dict' object has no attribute 'decode'

following this advice

Convert bytes to a string?

what do I need to do here

Upvotes: 2

Views: 1120

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195418

You can specify default= parameter (doc), where you decode bytes object from utf-8 (function specified in decode= parameter gets called for objects that can’t otherwise be serialized):

import json
f={'file_id': '2019_08_02_12_05_30.893834.csv', 'file_rows': [[b'Borough,OnStreet,CrossStreetOne,CrossStreetTwo,Asset\r\n'], [b'Brooklyn,Hoyt Street,Schermerhorn Street,Livingston Street,CityBench\r\n'], [b'Manhattan,HUDSON ST,BROOME ST,DOMINICK ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,CHARLTON ST,KING ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,CHARLTON ST,KING ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,KING ST,W HOUSTON ST,Bike Rack\r\n'], [b'Manhattan,HUDSON ST,SPRING ST,VAN DAM ST,Bike Rack\r\n'], [b'Bronx,CROSS BRONX EP,HUGH J GRANT CI,VIRGINIA AV,Bike Rack\r\n'], [b'Brooklyn,JACKSON ST,HUMBOLDT ST,WOODPOINT RD,Bike Rack\r\n']]}

def decode_bytes(o):
    return o.decode('utf-8')

print(json.dumps(f, default=decode_bytes, indent=4))

Prints:

{
    "file_id": "2019_08_02_12_05_30.893834.csv",
    "file_rows": [
        [
            "Borough,OnStreet,CrossStreetOne,CrossStreetTwo,Asset\r\n"
        ],
        [
            "Brooklyn,Hoyt Street,Schermerhorn Street,Livingston Street,CityBench\r\n"
        ],
        [
            "Manhattan,HUDSON ST,BROOME ST,DOMINICK ST,Bike Rack\r\n"
        ],
        [
            "Manhattan,HUDSON ST,CHARLTON ST,KING ST,Bike Rack\r\n"
        ],
        [
            "Manhattan,HUDSON ST,CHARLTON ST,KING ST,Bike Rack\r\n"
        ],
        [
            "Manhattan,HUDSON ST,KING ST,W HOUSTON ST,Bike Rack\r\n"
        ],
        [
            "Manhattan,HUDSON ST,SPRING ST,VAN DAM ST,Bike Rack\r\n"
        ],
        [
            "Bronx,CROSS BRONX EP,HUGH J GRANT CI,VIRGINIA AV,Bike Rack\r\n"
        ],
        [
            "Brooklyn,JACKSON ST,HUMBOLDT ST,WOODPOINT RD,Bike Rack\r\n"
        ]
    ]
}

Upvotes: 2

Related Questions