0

I have a socket in C sending this char[] (String) through a TCP socket:

{"time":0, "latitude":0.0000000, "longitude":0.0000000, "heading":0.000000, "roll":0.000, "pitch":0.000, "yaw":0.000}

It seems like it should be recognized as valid JSON in my Python client, but running:

parsed = json.loads(sock.recv(1024).decode('utf-8'))

Generates the following error:

ValueError: Extra data: line 1 column 117 - line 1 column 1024 (char 117 - 1024

Maybe C is throwing in some extra bits that Python doesn't like.

Figuring this was the case, I trying calling decode('ascii', 'ignore') on the input String hoping this would strip these characters out, but nothing has worked so far.

Any help would be greatly appreciated!

6
  • what does it look like when you print it? Commented May 22, 2015 at 21:33
  • If I print sock.recv(1024).decode('utf-8') I get {"time":0, "latitude":0.0000000, "longitude":0.0000000, "heading":0.000000, "roll":0.000, "pitch":0.000, "yaw":0.000}, as I would expect. Commented May 22, 2015 at 21:35
  • 2
    @JamesTaylor: and what if you use print repr(sock.recv(1024).decode('utf-8'))? Commented May 22, 2015 at 21:36
  • Oh. I get the JSON String I want trailed by a lot of \x00\x00\x00 characters. These are probably the problem. Can I get rid of them? Commented May 22, 2015 at 21:39
  • Also, thanks! I had no idea the repr() function existed! Commented May 22, 2015 at 21:39

1 Answer 1

2

It is common to have to use:

.strip('\x00')

in Python to remove null terminators from C strings.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.