logicOnAbstractions
logicOnAbstractions

Reputation: 2600

Object of type complex is not JSon serializable: ignore expressions burried in a string?

So I have a massing csv dump that I need to parse to JSON. Among those csv fields, there are comment fields, similar to this one:

2999.99 + 90J

which I guess is meant as a price and a number of days, but then when I get to dumping this as JSON it shows up as

(2999.99+90j)

Which is a complex number and not json seriablizable. I guess I could try to find & replace all border cases, but I'd rather just tell json/python to ignore any complex number representation? Is there some way to do that? Or do I really need to go through all the data? A quick search tells me there are a least a few problematic comments like that, so I'd rather not....

Upvotes: 0

Views: 1741

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54733

It is something in your code that is doing this conversion. Here's how the standard modules work:

import csv
import json

# x.csv contains:
# one,two,333,444+44j,555

data = list(csv.reader(open('x.csv')))
print(data)

d = json.dumps(data, indent=4)
print(d)

Output:

[['one', 'two', '333', '444+44j', '555']]
[
    [
        "one",
        "two",
        "333",
        "444+44j",
        "555"
    ]
]

Upvotes: 1

Related Questions