1

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

data = Data("employee.json")
json.dumps(data)
nessy hamsa
  • 185
  • 1
  • 12

3 Answers3

1

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))
febinjoy
  • 78
  • 7
0

Because it's a Blaze Data object. Objects with their own functions and objects, you cannot fully encode to json. Read: How to make a class JSON serializable


If you just want to see variable information use pprint function.

Community
  • 1
  • 1
Maciej A. Czyzewski
  • 1,539
  • 1
  • 13
  • 24
0

If you want an interactive view of your JSON file, just evaluate its name. You'll get the equivalent of print data.__repr__()

from blaze import Data
from blaze.utils import example

data = Data(example('accounts.json'))
data

#    amount     name
# 0     100    Alice
# 1    -200      Bob
# 2     300  Charlie
# 3     400   Dennis
# 4    -500    Edith
pneumatics
  • 2,836
  • 1
  • 27
  • 27