0

Currently I'm using:

import json
jsonlist = ["data", "test", "row", "blah", "boo"]

with open('test.txt', "wb") as jsfile:
    jsfile.write(json.dumps(jsonlist))

My current output is:

["data", "test", "row", "blah", "boo"]

Idealing I'd like an iterated json file as seen below: (if that's correctly formated)

[1: "data",
2: "test",
3: "row",
4: "blah",
5: "boo"]

My actual data is rather large blocks of html, this is just an example of how I'm doing it...

Does anyone know how I can achieve this?

7
  • 5
    That is not valid JSON. What exactly are you trying to do? Commented Nov 18, 2013 at 19:19
  • @DanielRoseman hence my if that's correctly formatted comment, I'm trying to create a valid iterated json file using a list of items... Commented Nov 18, 2013 at 19:21
  • Have you tried the indent keyword? json.dumps(python_object, indent=4) Commented Nov 18, 2013 at 19:22
  • 2
    But what is an "iterated JSON file"? What does it mean? What would it look like? What would you use it for? Commented Nov 18, 2013 at 19:22
  • 1
    What do you mean, exactly, by "iterated"? Are you looking to output a dictionary with increasing numeric keys? Commented Nov 18, 2013 at 19:23

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

In relation to your second version (using pairs) I get the error: TypeError: <enumerate object at 0x028BFC60> is not JSON serializable
He meant to say list(enumerate(jsonlist, 1)).
@Robᵩ: Thanks. For some reason I forgot that enumerate was one of the few 3.x-style functions in 2.x, returning a custom iterator from the day it was added to the language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.