1

I have a python dictionary which I want to convert into json.

Python dictionary:

{"20090209.02s1.1_sequence.txt": [645045714, 3559.6422951221466, 206045184], "20090209.02s1.2_sequence.txt": [645045714, 3543.8322949409485, 234618880]}

Desired output:

{
       "file_name":"20090209.02s1.1_sequence.txt",
       "file_information": [645045714, 3559.6422951221466, 206045184],

    {
       "file_name":"20090209.02s1.2_sequence.txt",
       "file_information": [645045714, 3543.8322949409485, 234618880],

    }
}

Tried with json.dumps but I didn't get the desired output.

5
  • @AndyHayden: There is more going on here. This is not a pretty-printing problem. Commented Jun 18, 2013 at 11:12
  • 2
    Your output is invalid JSON; did you mean for this to be a list of dictionaries instead? Commented Jun 18, 2013 at 11:12
  • @MartijnPieters eeew what is it?! Commented Jun 18, 2013 at 11:14
  • @AndyHayden: I've made an educated guess below. Let's see if that is good enough. Commented Jun 18, 2013 at 11:15
  • Yes the desire output I mentioned was invalid. The answer given by @Icfseth was what I wanted. Commented Jun 18, 2013 at 11:15

2 Answers 2

16

You need to first create a structure with the correct format:

import json

dict_ = {"20090209.02s1.1_sequence.txt": [645045714, 3559.6422951221466, 206045184], "20090209.02s1.2_sequence.txt": [645045714, 3543.8322949409485, 234618880]}
values = [{"file_name": k, "file_information": v} for k, v in dict_.items()]
json.dumps(values, indent=4)

Note that the desired JSON output does not look valid to me. Here's the output for this code:

[
    {
        "file_name": "20090209.02s1.1_sequence.txt", 
        "file_information": [
            645045714, 
            3559.6422951221466, 
            206045184
        ]
    }, 
    {
        "file_name": "20090209.02s1.2_sequence.txt", 
        "file_information": [
            645045714, 
            3543.8322949409485, 
            234618880
        ]
    }
]
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this is what I want. I will accept your answer whenever I can.
1

Split out your key-value pairs into separate dictionaries:

json.dumps([{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()])

Note that the order of your output will be arbitrary (dictionaries have no fixed ordering). You may want to sort the output to produce a predictable list:

from operator import itemgetter

data = [{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()]
data.sort(key=itemgetter('file_name'))
json.dumps(data)

This produces:

>>> data = [{'file_name': key, 'file_information': value} for key, value in yourdict.iteritems()]
>>> data.sort(key=itemgetter('file_name'))
>>> json.dumps(data)
'[{"file_name": "20090209.02s1.1_sequence.txt", "file_information": [645045714, 3559.6422951221466, 206045184]}, {"file_name": "20090209.02s1.2_sequence.txt", "file_information": [645045714, 3543.8322949409485, 234618880]}]'
>>> print json.dumps(data, indent=4)

[
    {
        "file_name": "20090209.02s1.1_sequence.txt",
        "file_information": [
            645045714,
            3559.6422951221466,
            206045184
        ]
    },
    {
        "file_name": "20090209.02s1.2_sequence.txt",
        "file_information": [
            645045714,
            3543.8322949409485,
            234618880
        ]
    }
]

4 Comments

Hm I want to add those keys called file_name and file_information.
I think you're missing the point here, the dict format and the json format are different :)
Yes, this is what I wanted. +1
I'd upvote as it is the same solution as lcfseth, but everything on one line ? really ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.