Search code examples
pythonblaze

How to convert blaze data objects to json?


While trying to use json.dumps in blaze error occurs saying TypeError: object is not JSON serializable.

data = Data("employee.json")
json.dumps(data)

Solution

  • You can't directly convert it to JSON.

    Alternate way is as follows :

    fields = [] # Create an empty list to hold the field names
    for fieldName in data.fields: # Iterate the field names 
        fields.append(fieldName) # Add to the list
    
    result = []        
    for row in data: # Iterate each row
        currentRow = {} 
        count = 0 
        for value in row: 
            currentRow[fields[count]] = value # Add each value with corresponding key from fields
            count = count + 1
        result.append(currentRow) 
    print(json.dumps(result))