0

I am getting error Traceback (most recent call last): json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

My code: ```

HOST = '0.0.0.0'
PORT = 10001

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            dataByte = conn.recv(1024)
            if not dataByte: 
                print("Not data")
            myStr = dataByte.decode('utf-8').replace("'", '"')
        
            data = json.loads(myStr)
            s = json.dumps(data, indent=4, sort_keys=True)
            print(s)
```

Original dataByte b'POST / HTTP/1.1\r\nHost: localhost:10001\r\nUser-Agent: Go-http-client/1.1\r\nContent-Length: 3153\r\nContent-Type: application/json\r\nAccept-Encoding: gzip\r\n\r\n{"Event":"error","S":"0\ufffd\u000380\ufffd\u0002 \ufffd\u0003\u0002\u0001\u0002\u0002\u0014\u0018DIv\ufffd\ufffd\xca\xb9\ufffd\ufffd\ufffd\ufffdSJa~\ufffd\ufffd\ufffd10\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u000b\u0005\u00000O1\u000b0\t\u0006\u0003U\u0004\u0006\u0013\u0002BR1\u000e0\u000c\u0006\u0003U\u0004\u0008\u000c\u0005AAAAA1\r0\u000b\u0006\u0003U\u0004\u0007\u000c\u0004BBBB1!0\u001f\u0006\u0003U\u0004\n\u000c\u0018Internet Widgits Pty Ltd0\u001e\u0017\r210524192942Z\u0017\r230827192942Z0!1\u000b0\t\u0006\u0003U\u0004\u0006\u0013\u0002GB1\u00120\u0010\u0006\u0003U\u0004\u0003\u000c\tlocalhost0\ufffd\u0001\"0\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u0001\u0005\u0000\u0003\ufffd\u0001\u000f\u00000\ufffd\u0001\n\u0002\ufffd\u0001\u0001\u0000\ufffdy\u0017\u0012|\ufffd\u0007\ufffd\u0006\ufffdwl\ufffd\ufffd\ufffdhu\ufffdAP\u000e\ufffd\ufffda\ufffd\u' b'0016\ufffdU\ufffdp\ufffd?\ufffd\ufffd\ufffdq2\u0000\ufffd\ufffd\ufffd9\ufffd\x7f\ufffd\ufffd\ufffd\ufffd%\ufffd\ufffd\ufffd\ufffd^B\ufffd\u0026*\ufffd\ufffd\ufffd\ufffd+\xd7\x98\ufffd\ufffdHw\ufffdl\ufffd\ufffd\ufffd\ufffd\xe3\x90\x95u\ufffdV\ufffd\xd6\x9d\ufffd\u0000d\ufffd\ufffd\xcd\x92\ufffd\u003c\ufffd\ufffd\ufffd\ufffdn%\ufffd-\u001e\ufffd\u00140ml\u0013A\ufffd#\ufffdr0$=\ufffd\ufffd\ufffd\ufffdr\ufffd\ufffd\ufffd\ufffd_|\ufffd\ufffd\ufffd\ufffdM\ufffd\t\ufffdK~\ufffdZ\ufffd\ufffd8\xd7\x9d\u0003\xc5\xb0\ufffdo\ufffd\ufffd\ufffd3\xca\x882\u003e\ufffd\x7fj\ufffdc\ufffd\ufffdtuW\ufffdp\ufffd\ufffd\xcd\xaa.lc\ufffd\ufffd\ufffd\u00058\u001c\u001a,th3D4m\ufffd\u000b(\u0026\ufffdq\ufffd{\ufffd\ufffd\ufffd\ufffd.\u003c\u000c\u003c\ufffd[)\xd2\x92\ufffd]\ufffd\ufffd\u0019t\u001e;E\ufffd%\ufffd\"\ufffd\ufffdM\ufffd/\u0001w\ufffdZ\u000c\ufffd\xc8\x85b\ufffdR\ufffdaT\ufffdVu\ufffd+\ufffd\ufffdU\ufffd;\u0019O\u0011\u0002\u0003\u0001\u0000\u0001\ufffd:080\u000b\u0006\u0003U\u001d\u000f\u0004\u0004\u0003\u0002\u000400\u0013\u0006\u0003U\u001d%\u0004\u000c0\n\u0006\u0008+\u0006\u0001\u00' b'05\u0005\u0007\u0003\u00020\u0014\u0006\u0003U\u001d\u0011\u0004\r0\u000b\ufffd\tlocalhost0\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u000b\u0005\u0000\u0003\ufffd\u0001\u0001\u0000\ufffdV\ufffd.\ufffdT\ufffdl\ufffd\ufffd_\xd9\x93\u0015\ufffd\ufffdqJ\ufffd\ufffd\ufffd9\ufffdW\ufffd\u00105\xdc\xa2\ufffd\ufffd:\ufffd\ufffdyBTJ\ufffd\ufffdF\ufffd\u0003\ufffd\ufffd\u001a#\ufffd\u0018\u0014S(C\ufffd\ufffd\ufffd\u000cWY\u0018\ufffd\u0026]\ufffd!6\ufffd\ufffd\ufffd}\ufffd\u0000Q\ufffd\ufffdF\u0001\ufffd\ufffd)\xd7\xb1\u001ar\ufffd\ufffdF\ufffd4y\u0007\ufffd\u0004\ufffd\u001b\ufffd@\ufffd\u0018\ufffdh\ufffdW\ufffd}\ufffd:\ufffdD\ufffd\ufffdAY\ufffd\ufffdK\ufffd\ufffdP\ufffd\ufffdB\u0008\ufffd\ufffdV+\ufffdo\ufffd\ufffd\ufffd\ufffd\ufffd\xd4\x89itCP\ufffd?\ufffd\ufffd\ufffd\u0002\ufffdZ\u0010\ufffd04\x7fIR\ufffd\ufffd\ufffd\ufffd%;D\ufffd\ufffd\u001d\ufffdUrB\ufffd)z\ufffdZ\ufffdb\ufffd\ufffd\ufffd\ufffd\ufffd\xce\xba\ufffdE\xde\xa5\ufffd\u000f\ufffd\ufffdOx\ufffdi\ufffd%\x7f\u0014\ufffd\ufffd\ufffdZ\ufffd\ufffd\ufffdC\ufffduj\ufffd\ufffd\ufffd\u001c\nmAt\u' b'fffd\ufffd\u001e\ufffd]\"XWX\ufffdv}\ufffd\ufffd\ufffdT9@HP\ufffd\ufffd\u001c\ufffd\ufffdF\u0004\ufffd$F\xce\x8b\ufffd\ufffd\ufffd\ufffd\ufffdI","Message":"An error has been occured: ERROR 505"}'

my str {"Event":"error","S":"0\ufffd\u000380\ufffd\u0002 \ufffd\u0003\u0002\u0001\u0002\u0002\u0014\u0018DIv\ufffd\ufffdʹ\ufffd\ufffd\ufffd\ufffdSJa~\ufffd\ufffd\ufffd10\r\u0006\t*\ufffdH\ufffd\ufffd\r\u0001\u0001\u000b\u0005\u00000O1\u000b0\t\u0006\u0003U\u0004\u0006\u0013\u0002BR1\u000e0\u000c\u0006\u0003U\u0004\u0008\u000c\u0005AAAAA1\r0\u000b\u0006\u0003U\u0004\u0007\u000c\u0004BBBB1!0\u001f\u0006\u0003U\u0004\n\u000c\u0018I\ufffdI","Message":"An error has been occured: ERROR 505"}

Fabian
  • 11
  • 3
  • 1
    Check `myStr` before trying to convert it, it's probably empty... – Thierry Lathuille Jul 30 '21 at 14:05
  • It is not empty. – Fabian Jul 30 '21 at 14:08
  • 1
    Replacing `'` with `"` to make valid JSON is fragile, at best. If you are really receiving Python `dict` literals, use `ast.literal_eval` instead (or better yet, fix the sender to send proper JSON). – chepner Jul 30 '21 at 14:16
  • As an aside, `if not dataByte: ` means that the connection has been closed and you'll never receive data again. Its a good time to break out of the loop. – tdelaney Jul 30 '21 at 14:20
  • Is `dataByte` really UTF-8-encoded, or UTF-16-encoded? It's not clear if all those `\ufffd` characters are literal 6-byte sequences in the resulting "JSON", or if those are part of the Python representation of the `bytes` value. – chepner Jul 30 '21 at 14:20

1 Answers1

0

Your string seems to contain mangled Unicode escapes. The escape \u is followed by four characters encoding the codepoint, so, for example, \u000380 is parsed as \u0003 (aka ascii Control-C) followed by the ordinary digits "80".

In any event when I tried to reproduce your problem, I got a different error than you did:

json.decoder.JSONDecodeError: Invalid control character at: line 1 column 25 (char 24)

Since there is no telling what else may be going on, please create a test string containing the response data you are trying to parse and embed it in your Python code (edit your question, don't try to do it in a comment).

We only need code with the data string and the json.loads() call... and the error message. Then we may be able to see what you see, and make progress.

alexis
  • 48,685
  • 16
  • 101
  • 161