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"
}
001
  • 13,291
  • 5
  • 35
  • 66
  • What have you accomplished so far? Where are you stuck? Maybe start here: [Reading and Writing JSON through Python](https://stackoverflow.com/a/45791955) – 001 Oct 06 '22 at 20:39

1 Answers1

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.