I want this string:
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"78cb7b69-bbfa-4d6c-8156-ada66201bf73","id_v1":"/sensors/22","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"4b16b918-485a-44de-82aa-4ff467f6591a","rtype":"device"},"type":"motion"}],"id":"813e2ed1-f28e-451b-9ac6-9eef76ef7b4a","type":"update"},{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"6a743cb9-bcc4-44bb-8592-c4854e8fadcb","id_v1":"/sensors/32","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"cdb31512-997f-4e26-80d1-50dca6b431a3","rtype":"device"},"type":"motion"}],"id":"240698ea-5938-4e7c-a70c-75bad0fe2a7f","type":"update"},{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"f4fc5daf-a2aa-4c9f-9812-a65c9922b53e","id_v1":"/sensors/2","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"8daa62b1-af26-44b3-8356-15d21cf6642c","rtype":"device"},"type":"motion"}],"id":"124546d2-cf7e-4b64-a99d-2af2c047aaea","type":"update"}
To be split up in a list as follows:
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"78cb7b69-bbfa-4d6c-8156-ada66201bf73","id_v1":"/sensors/22","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"4b16b918-485a-44de-82aa-4ff467f6591a","rtype":"device"},"type":"motion"}],"id":"813e2ed1-f28e-451b-9ac6-9eef76ef7b4a","type":"update"},
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"6a743cb9-bcc4-44bb-8592-c4854e8fadcb","id_v1":"/sensors/32","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"cdb31512-997f-4e26-80d1-50dca6b431a3","rtype":"device"},"type":"motion"}],"id":"240698ea-5938-4e7c-a70c-75bad0fe2a7f","type":"update"},
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"f4fc5daf-a2aa-4c9f-9812-a65c9922b53e","id_v1":"/sensors/2","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"8daa62b1-af26-44b3-8356-15d21cf6642c","rtype":"device"},"type":"motion"}],"id":"124546d2-cf7e-4b64-a99d-2af2c047aaea","type":"update"}
So I need the full strings for further breakdown.
I tried many things, Google, stackoverflow etc. I do can search the 'creationtime' but the rest is omitted whatever I try. I guess I need some kind of non-greedy RE? Anyhow - It just won't work for me.
Anyone - some help would be highly appreciated.
CodePudding user response:
Your input data looks like a json array but without the square brackets []
.
Trying to decode your input data into a json object -
import json
data = '''{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"78cb7b69-bbfa-4d6c-8156-ada66201bf73","id_v1":"/sensors/22","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"4b16b918-485a-44de-82aa-4ff467f6591a","rtype":"device"},"type":"motion"}],"id":"813e2ed1-f28e-451b-9ac6-9eef76ef7b4a","type":"update"},{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"6a743cb9-bcc4-44bb-8592-c4854e8fadcb","id_v1":"/sensors/32","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"cdb31512-997f-4e26-80d1-50dca6b431a3","rtype":"device"},"type":"motion"}],"id":"240698ea-5938-4e7c-a70c-75bad0fe2a7f","type":"update"},{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"f4fc5daf-a2aa-4c9f-9812-a65c9922b53e","id_v1":"/sensors/2","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"8daa62b1-af26-44b3-8356-15d21cf6642c","rtype":"device"},"type":"motion"}],"id":"124546d2-cf7e-4b64-a99d-2af2c047aaea","type":"update"}'''
data = '[' data ']'
decoded = json.loads(data)
for item in decoded:
print(json.dumps(item))
Output:
{"creationtime": "2022-11-25T09:12:44Z", "data": [{"id": "78cb7b69-bbfa-4d6c-8156-ada66201bf73", "id_v1": "/sensors/22", "motion": {"motion": true, "motion_valid": true}, "owner": {"rid": "4b16b918-485a-44de-82aa-4ff467f6591a", "rtype": "device"}, "type": "motion"}], "id": "813e2ed1-f28e-451b-9ac6-9eef76ef7b4a", "type": "update"}
{"creationtime": "2022-11-25T09:12:44Z", "data": [{"id": "6a743cb9-bcc4-44bb-8592-c4854e8fadcb", "id_v1": "/sensors/32", "motion": {"motion": true, "motion_valid": true}, "owner": {"rid": "cdb31512-997f-4e26-80d1-50dca6b431a3", "rtype": "device"}, "type": "motion"}], "id": "240698ea-5938-4e7c-a70c-75bad0fe2a7f", "type": "update"}
{"creationtime": "2022-11-25T09:12:44Z", "data": [{"id": "f4fc5daf-a2aa-4c9f-9812-a65c9922b53e", "id_v1": "/sensors/2", "motion": {"motion": true, "motion_valid": true}, "owner": {"rid": "8daa62b1-af26-44b3-8356-15d21cf6642c", "rtype": "device"}, "type": "motion"}], "id": "124546d2-cf7e-4b64-a99d-2af2c047aaea", "type": "update"}
Here we are decoding the json string into a json object using json.loads()
and then after getting each object in the array, encoding it again in a json string using json.dumps()
.
You can also access the nested items in the json object. e.g. -
for item in decoded:
print('creationtime', item['creationtime'])
print('motion', item['data'][0]['motion'])
Output:
creationtime 2022-11-25T09:12:44Z
motion {'motion': True, 'motion_valid': True}
creationtime 2022-11-25T09:12:44Z
motion {'motion': True, 'motion_valid': True}
creationtime 2022-11-25T09:12:44Z
motion {'motion': True, 'motion_valid': True}
CodePudding user response:
If you don't have split pattern simply create one.It's all in our hands
st = "Your string"
st = st.replace('"update"},','"update"},|')
lis = st.split("|")
print(lis)
Gives
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"78cb7b69-bbfa-4d6c-8156-ada66201bf73","id_v1":"/sensors/22","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"4b16b918-485a-44de-82aa-4ff467f6591a","rtype":"device"},"type":"motion"}],"id":"813e2ed1-f28e-451b-9ac6-9eef76ef7b4a","type":"update"},
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"6a743cb9-bcc4-44bb-8592-c4854e8fadcb","id_v1":"/sensors/32","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"cdb31512-997f-4e26-80d1-50dca6b431a3","rtype":"device"},"type":"motion"}],"id":"240698ea-5938-4e7c-a70c-75bad0fe2a7f","type":"update"},
{"creationtime":"2022-11-25T09:12:44Z","data":[{"id":"f4fc5daf-a2aa-4c9f-9812-a65c9922b53e","id_v1":"/sensors/2","motion":{"motion":true,"motion_valid":true},"owner":{"rid":"8daa62b1-af26-44b3-8356-15d21cf6642c","rtype":"device"},"type":"motion"}],"id":"124546d2-cf7e-4b64-a99d-2af2c047aaea","type":"update"}