1

I'm trying to parse a json file into a python dict (to make it copy-ready for Redshift).

My intended output format is:

{col1:val1, col2:val2,..}
{col1:val1, col2:val2,..}

The current format of the file is:

{"val0": {"col1":"val1", "col2":"val2",..},
 "val0": {"col1":"val1", "col2":"val2",..},..}

Where "val0" is a date field (only value, no column name) that I don't need in my output.

How do I convert the latter format to the former? I've tried going through documentation for the json module (as well as a few other StackOverflow answers), but nothing seems to click. Any help is appreciated. Thanks!

5
  • 1
    The json module is definitely the way to go here. Why isn't it working? Commented Jul 22, 2016 at 21:56
  • Do you care about the order of lines in your intended output forma? Commented Jul 22, 2016 at 21:56
  • Have you tried this? Please post your code. Commented Jul 22, 2016 at 21:57
  • The code I'm using is f = open("<PATH>/jsoninput.json","r").read() import json parsed_json = json.loads(f) print parsed_json I just get the same format as before. @SuperSaiyan: The order of lines doesn't particularly matter. Only that the order within each line stays consistent (I'll be loading the output file into redshift tables daily). Commented Jul 25, 2016 at 15:24
  • 1
    @MNav: Please update your question with it. Also, you need to show us what you have tried relevant to the question being asked. Not just how use parse the JSON. Commented Jul 25, 2016 at 16:35

1 Answer 1

1

It sounds like you need a json array.

import json


with open('example.json', 'r') as f:
    data = json.load(f)

array_data = list(data.values())

print(array_data)  # [{'col1': 'val1', 'col2': 'val2'},
                   #  {'col1': 'val1', 'col2': 'val2'}]

with open('array_result.json', 'w') as f:
    json.dump(array_data, f)
Sign up to request clarification or add additional context in comments.

Comments

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.