2

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

isherwood
  • 58,414
  • 16
  • 114
  • 157
ziggy
  • 1,488
  • 5
  • 23
  • 51

1 Answers1

2

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"
        ]
    ]
}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91