0

I would like fetch data from a SQL Server database and transform the result in a JSON format.

Thats not difficult, but one column is already a JSON and I would like separate it, but the result is a little bit confusing.

My code:

    rows = cursor.fetchall() 
    print (rows)
    objects_list = []
    for row in rows:
        d= collections.OrderedDict()
        #d["Bestelldatum"]= row[0].strftime("%Y-%m-%d %H:%M")
        a = json.loads(row[0])
        #print(a)
        #d["Adresse"] = row[0]
        #d["Tor"] = a["tor"]
        #d["Stiege"] = a["stg"]
        #d["Stock"] = a["stk"] 
        #d["Tür"] = a["tür"]
        #d["PLZ"] = a["plz"]

        objects_list.append(d)
    j = json.dumps(objects_list, ensure_ascii=False)
    print (j)

My problem is row 1, that is a JSON, see the pic

Picture from database

If I run it so, I get this error messange

in loads return _default_decoder.decode(s) File "C:\Users\acas1\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Users\acas1\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I guess something is wrong with the JSON data which I get from the database I don't understand why some data have backslashes, because at SQL they not exist. Thats the result if I fetch just the raw data from the database:

Picture from result in python

I tried everything that they say in this post JSONDecodeError: Expecting value: line 1 column 1 (char 0) but nothing helps.

I hope someone have an idea

Shajko
  • 107
  • 2
  • 8

0 Answers0