0

I am new to the world of programming, I need help to transform one JSON file to another format using python. The Input file has a key and a value variable which I need to transform so that the output file has values. Pls(refer below) :

Input File :

[
    {
        "variable_type": "env_var",
        "key": "TEST_VARIABLE_1",
        "value": "TEST_1",
        "protected": false,
        "masked": true,
        "environment_scope": null
    },
    {
        "variable_type": "env_var",
        "key": "TEST_VARIABLE_2",
        "value": "TEST_2",
        "protected": false,
        "masked": false,
        "environment_scope": "*"
    }
]

Output file:

{
    "TEST_VARIABLE_1":"TEST_1",
    "TEST_VARIABLE_2":"TEST_2"
}
1

1 Answer 1

1

You can do something like

output_file_data = {}
for dictionary in input_file_data:
   output_file_data[dictionary["key"]] = dictionary["value"]

Hope it helps.

Note: This will fail when your dictionary array does not contain a "key" o "value" field. Is up to you to define how your program will manage this edge case.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.