Home > Back-end >  TypeError: Object of type filter is not JSON serializable - Nested Object
TypeError: Object of type filter is not JSON serializable - Nested Object

Time:11-07

import csv
import json 

def csv_to_json(csvFilePath):
    jsonArray = []
    
    decoded_file = csvFilePath.read().decode('utf-8').splitlines()
   
    #load csv file data using csv library's dictionary reader
    csvReader = csv.DictReader(decoded_file) 
    #convert each csv row into python dict
    for row in csvReader: 
        #add this python dict to json array
        option = [row['option1'], row['option2'], row['option3'], row['option4']]
        option = filter(None, option)
        newJson = {
          'numb': row['numb'],
          'question': row['question'],
          'answer': row['answer'],
          'options': option
        }
        jsonArray.append(newJson)

    print('>>>>>>>jsonArray>>>>>>>', jsonArray)

    jsonString = json.dumps(jsonArray)

    print('>>>>>>>jsonString>>>>>>>', jsonString)

    return jsonArray

Error

TypeError at /path/path
Object of type filter is not JSON serializable

>>>>>>>jsonArray>>>>>>> [{'numb': '1', 'question': 'What does HTML stand for?', 'answer': 'Hyper Text Markup Language', 'options': <filter object at 0x7f3d3459fa00>}, {'numb': '2', 'question': 'What does CSS stand for?', 'answer': 'Cascading Style Sheet', 'options': <filter object at 0x7f3d34317400>}]

If i comment out # 'options': option from newJson then it works perfectly

CodePudding user response:

As the error says, a filter object is not serializable.

But a list is

'options': list(option)

However, it's not really clear to me what you're filtering. A list with None types will become null in the JSON array

CodePudding user response:

JSON does not know how to convert a filter object into a JSON string. In order to put option into the JSON string, you need to convert it to a list first:

import csv
import json 

def csv_to_json(csvFilePath):
    jsonArray = []
    
    decoded_file = csvFilePath.read().decode('utf-8').splitlines()
   
    # load csv file data using csv library's dictionary reader
    csvReader = csv.DictReader(decoded_file) 
    # convert each csv row into python dict
    for row in csvReader: 
        # add this python dict to json array
        option = [row['option1'], row['option2'], row['option3'], row['option4']]
        option = list(filter(None, option))
        newJson = {
          'numb': row['numb'],
          'question': row['question'],
          'answer': row['answer'],
          'options': option
        }
        jsonArray.append(newJson)

    print('>>>>>>>jsonArray>>>>>>>', jsonArray)

    jsonString = json.dumps(jsonArray)

    print('>>>>>>>jsonString>>>>>>>', jsonString)

    return jsonArray

CodePudding user response:

The built-in function filter() has no default Encoder as per documentation https://docs.python.org/3/library/json.html. You need to implement custom Encode Decoder depending on your need what you want store and retrieve. Another helpful link How to make a class JSON serializable

  • Related