It's rarely worth thinking of how to convert one JSON representation into another; instead, convert one Python object into another, then represent that in JSON.
So, for example, given this Python list:
["data", "test", "row", "blah", "boo"]
… maybe you want this dict:
{1: "data", 2: "test", 3: "row", 4: "blah", 5: "boo"}
… which will JSONify to:
'{"1": "data", "2": "test", "3": "row", "4": "blah", "5": "boo"}'
Note that the keys have been turned into strings. The keys of JSON objects must be strings. If that's not what you want, you don't want JSON objects. Maybe you want a list of pairs in that case, which will turn into a JSON array of JSON arrays (since arrays can have numbers as values)?
enumerate can turn the list into a list of (index, value) pairs, and dict, can turn a list of (key, value) pairs into a dict:
jsonlist = ["data", "test", "row", "blah", "boo"]
jsondict = dict(enumerate(jsonlist, 1))
with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsondict))
Or, if you just wanted an array of arrays, stop at the enumerate. Because enumerate actually gives you an iterator over the values, rather than a sequence, you'll have to wrap it in list:
jsonlist = ["data", "test", "row", "blah", "boo"]
jsonpairs = list(enumerate(jsonlist, 1))
with open('test.txt', "wb") as jsfile:
jsfile.write(json.dumps(jsonpairs))
Note that, because Python uses 0-based indexing, I had to pass a start argument to enumerate to count from 1 instead of 0.
if that's correctly formattedcomment, I'm trying to create a valid iterated json file using a list of items...indentkeyword?json.dumps(python_object, indent=4)