Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes to json
#1
Hi
I am trying to send json data over a pipe but cannot get it quite right
Send
mesages = [{"id": "1", "txt": "message 1", "start_date": "2014-02-09", "start_time": "16:20", "duration": "01:00"}]
        win32file.WriteFile(handle,     str.encode(json.dumps(mesages), encoding="ascii"))
receive
 result, data = win32file.ReadFile(p, 65535)
            json_data = json.dumps(data.decode('ascii'))
            print(json_data['first_name'])
This gives me this error
[error
TypeError: string indices must be integers
[/error]

What is the correct way of doing this?
Reply
#2
Shouldn't you be calling loads, rather than dumps to decode the JSON string into Python objects?
Reply
#3
I still get TypeError: string indices must be integers
Reply
#4
import json

mesages = [{"id": "1", "txt": "message 1", "start_date": "2014-02-09", "start_time": "16:20", "duration": "01:00"}]
payload = json.dumps(mesages).encode("ascii")
print(payload)
print(type(payload))

# json can also loads bytes
# but here the encoding is not explicit
data = json.loads(payload)

data2 = json.loads(payload.decode("ascii"))

print(data2)
Output:
b'[{"id": "1", "txt": "message 1", "start_date": "2014-02-09", "start_time": "16:20", "duration": "01:00"}]' <class 'bytes'> [{'id': '1', 'txt': 'message 1', 'start_date': '2014-02-09', 'start_time': '16:20', 'duration': '01:00'}]
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
Hi
Yes this works, but I am reading from a pipe using
result, data = win32file.ReadFile(p, 65535)
This gives me the string
b'"{\\"success\\": \\"true\\", \\"status\\": 200, \\"message\\": \\"Hello\\"}"'
I then decode
d = data.decode('ascii')
Then I do a json load
json_data = json.loads
but when I try to access as a json object I get the error

TypeError: string indices must be integers
Reply
#6
How can access the result as a json object

result, data = win32file.ReadFile(p, 65535)
json_data = json.loads(data)
print(json_data['success'])
- This gives TypeError: string indices must be integers
Reply
#7
this will work
import json
spam = b'"{\"success\": \"true\", \"status\": 200, \"message\": \"Hello\"}"'
print(spam.decode())
data = json.loads(spam.decode()[1:-1])
print(data['success'])
but I think there is problem with the data being send/receive - i.e. extra quotes. Probably on the sending site you make too many conversions
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
Here is my sending code
s = '{"success": "true", "status": 200, "message": "Hello"}'
win32file.WriteFile(handle,     str.encode(json.dumps(s)))
Reply
#9
yep, s is already a JSON string, no need to dump it again

win32file.WriteFile(handle, s.encode())
or

s = {"success": "true", "status": 200, "message": "Hello"}
win32file.WriteFile(handle, json.dumps(s).encode())
then on receiving end

data = json.loads(spam.decode())
Also, didn't work with win32file.WriteFile, not sure if you can skip also encode/decode part
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
That was it - thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replace bytes with other byte or bytes BigOldArt 1 10,520 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020