Python – Stores strings to the JSON object Python

Stores strings to the JSON object Python… here is a solution to the problem.

Stores strings to the JSON object Python

I’ve read a lot about this but can’t find any questions about str.

I get a very long string that contains a 16-byte name followed by a 4-byte number, repeated for N individuals. An example is as follows:

* EDIT: 1) The string is the message

2) Add a microphone to the expected output

msg = 'George\0\0\0\0\0\0\0\0\0\0' + '0095' + 'Mikeeeeeeeeeeee\0' + '0100' + 'Kelly\0\0\0\0\0\0\0\0\0\0\0' + '0000'

Now I need to store these data in JSON objects. I’ve tried looping, but it always rewrites the previously stored data. What I want is shown below, but for longer strings, because writing msg[start:end] for each data is completely retarded.

data = {}
data[msg[0:16]] = {
    "marks" : msg[16:20]
data[msg[20:36]] = {
    "marks" : msg[36:40]
data[msg[40:56]] = {
    "marks" : msg[56:60]

Expected output:

"George\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000": {
    "marks": "0095"
"Kelly\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000": {
    "marks": "0000"
 "Mikeeeeeeeeeeee\u0000": {
    "marks": "0100"



Let’s say you want details of all objects, namely George, Mike, and Kelly in your data, and your msg is only 76 in length when you access 60 and later. You should make a nested JSON based on the output you want, for example:

length = len(msg)
i = 0
data = {}
while i < length:
    data[msg[i:i+16]] = {"marks" : msg[i+16:i+20]}
    i += 20
print data


{'George\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00': {'marks': '0095'}, 'Mikeeeeeeeeeeee\x00': {'marks': '0100'}, 'Kelly\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\ x00': {'marks': '0000'}}

Hope that helps

Related Problems and Solutions