import json
b={'description': [['hostname ', ' ABC'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['used_disk ', ' 486G'], ['avail_disk ', ' 1.9T'], ['percentage ', ' 21'], ['mount_disk ', ' /data'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['used_disk ', ' 533G'], ['avail_disk ', ' 1.8T'], ['percentage ', ' 23%'], ['mount_disk ', ' /data'], ['hostname ', ' MNOP'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['used_disk ', ' 664G'], ['avail_disk ', ' 1.7T'], ['percentage ', ' 29%'], ['mount_disk ', ' /data']]}
b=(b['description'])
size = len(b)
idx_list = [idx + 1 for idx, val in enumerate(b) if 'mount_disk ' in val] #note the blank space!!!!
res = [dict(b[i: j]) for i, j in
zip([0] + idx_list, idx_list +
([size] if idx_list[-1] != size else []))]
JSONarray=json.dumps(res) # or json.dump(...) to write the output in a new file
to check your new array of JSON objects:
print("now you have this list of JSON objects : " + str(res))
I would like to note that any feedback would be really appreciated, even just to clearify if you already have already solved your issue.
the split in multiple lists is adapted from: https://www.geeksforgeeks.org/python-split-list-into-lists-by-particular-value/
Edit:
I've now looked at your other questions in stackoverflow which make me think you weren't asking for fixing your inner lists to dicts but just a plain dict to json conversion.
I can't understand how a json in that format may help you, but if that's the case you should specify it in your question.
Looking at that dict format is very misleading.
Edit2
just:
store=[['hostname ', ' ABCD'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T'], ['hostname ', ' XYZ'], ['fsystem ', ' /dev/sdb'], ['actual_size ', ' 2.5T']]
size = len(store)
idx_list = [idx + 1 for idx, val in enumerate(store) if 'actual_size ' in val] #note the blank space!!!!
storeNew = [dict(store[i: j]) for i, j in zip([0] + idx_list, idx_list + ([size] if idx_list[-1] != size else []))]
storeNew #[{'actual_size ': ' 2.5T', 'fsystem ': ' /dev/sdb', 'hostname ': ' ABCD'},{'actual_size ': ' 2.5T', 'fsystem ': ' /dev/sdb', 'hostname ': ' XYZ'}]