Thomas Murphy
Thomas Murphy

Reputation: 1468

Python: JSON.dumps to structure { key: [ key: val ] }

I'm working a CSV file, and trying to write to JSON file from there with JSON.dumps. My desired data structure is

{
    "key1": {"innerkey1": "value1", … "innerkeyn": "valuen"},
    ...
    ...
    ...
}

however, JSON.dumps syntax seems to ask me to write

json.dumps({"key1": {"innerkey1": "value1", … "innerkeyn": "valuen"}})

which changes the meaning of the structure quite a bit (and isn't valid JSON because the dump is iterated over in a list comprehension.

I'm guessing I'm understanding dict syntax incorrectly, but can't quite pull this off.

EDIT: edited to change brace type. The issue remains that { "key1" … } is being written for each item, where I want one set of hogan braces to wrap around the entire structure, but

json.dumps("key1": {"innerkey1": "value1", … "innerkeyn": "valuen"}) 

throws a syntax error on the semicolon

EDIT2: json.dumps is called n-number of times, and each time is written to file. The output file looks like this:

{"key1": {"innerkey1": "value1", … "innerkeyn": "valuen"}},
{"key2": {"innerkey1": "value1", … "innerkeyn": "valuen"}}

this syntax makes JSON expect the comma at the end of the first enclosing set of hogans to signal end of file. The desired output is

{ 
    "key1": {"innerkey1": "value1", … "innerkeyn": "valuen"},
    "key2": {"innerkey1": "value1", … "innerkeyn": "valuen"}
}

Upvotes: 0

Views: 2401

Answers (3)

catchmeifyoutry
catchmeifyoutry

Reputation: 7389

If I understand correctly, you want to output in each iteration some 'partial' json string (which by itself is not valid json), e.g.

"key1": {"innerkey1": "value1", … "innerkeyn": "valuen"},

But json.dumps doesn't output 'partial json', only valid json.

So, either:

  • first collect all the results in a single dict for the whole document, and call json.dumps once.
  • or, write out the first and final document brackets and"key1": parts yourself, and use json.dumps only for the {"innerkey1": "value1", … "innerkeyn": "valuen"} part

Upvotes: 0

tzaman
tzaman

Reputation: 47840

It looks like you want dumps to only emit partial JSON for the inner keys, I don't think you can make it do that. I'd suggest either passing the entire dictionary (with all the top-level keys) to the dumps call so that it can write the whole thing, or taking care of the outer braces and keys manually and just using dumps for the inner dictionaries.

Alternatively, you can use json.dumps as you are, and strip off the starting and ending braces before writing them out (i.e. only print out the_json[1:-1]).

Upvotes: 1

mhlester
mhlester

Reputation: 23251

By your edit, you want one key1 to store many results?

{
    "key1": [{"innerkey1": "value1", … "innerkeyn": "valuen"},
             {"innerkey2": "value1", … "innerkeyn": "valuen"},
             {"innerkey3": "value1", … "innerkeyn": "valuen"}]
}

It's kind of a shot in the dark. And from the hip.

Upvotes: 0

Related Questions