Python JSON formatter -


i new python. starting experiment small programs. saw question :

input of json can of format :

'[ ["a","b","c"], [1,2,null], [null,3,4], [5,null,6] ]'  

or :

'[ { "a":1, "b":2 }, { "b":3, "c":4 }, { "c":6, "a":5 } ]' 

we should convert :

output = '{ "a": [1,null,5], "b": [2,3,null], "c": [null,4,6] }' 

so far can think of checking each element , appending result. there easy or better way in python. please enlighten me.

try python 3:

def get_elements(json_txt): import json arr = json.loads(json_txt) new = {} list_of_keys = [] list_of_keys_from_dicts = [list(elem.keys()) elem in arr] # getting keys json keys in list_of_keys_from_dicts:     key in keys:         if key not in list_of_keys:             list_of_keys.append(key) key in list_of_keys:     new[key] = [] element in arr:     key in list_of_keys:         if key in element:             new[key].append(element[key])         else:             new[key].append(none) return json.dumps(new) 

Comments

Popular posts from this blog

android - Why am I getting the message 'Youractivity.java is not an activity subclass or alias' -

python - How do I create a list index that loops through integers in another list -

c# - “System.Security.Cryptography.CryptographicException: Keyset does not exist” when reading private key from remote machine -