Joseph Wahba Joseph Wahba - 9 months ago 59
JSON Question

How to append JSON objects to a list?

I'm trying to append JSON objects to form a list of JSON objects.

def GenIndentifier(options):
return data

The data here is a valid JSON object as follows:

["27780741708467800000001", "e5073922dbb7a278769d52277d49c6ad3017b1ba"]

Afterwards, I loop through the GenIdentifier function to generate many JSON objects:

for i in range(0,2,1):
print data

Now the list of JSON data is not a valid JSON format because of some single quotations marks that pop up:


Any idea how to solve that?
I've searched and googled but in vain.

Answer Source

The trouble is that you are dumping your data to JSON inside GenIndentifier, then appending that JSON string to a list.

Instead you should move the JSON conversion out of that function, and do it at the end:

def GenIndentifier(options):
    data = [identifier, hex_dig]
    return data

data = []
for i in range(0, 2, 1):
data = json.dumps(data)
print data