How to call Global variables in nested JSON?

Options
Srini_E
Srini_E Registered Posts: 14

Hi,

I would like to call variables in nested dictionary/json.

Nested Global vars:

{
"set1": {
"input_file": "test/input.xlsx",
"output_fle": "test/output.xlsx",
},
"set2": {
"input_file": "test/input.xlsx",
"output_fle": "test/output.xlsx",
}
}

Global_vars_sets.PNG

In Recipe: I don't think this is the right way to call variables in nested json.

input_file = dataiku.get_custom_variables()["set1"]["input_file"]
print(input_file)

Could anyone please help me with this?

Thanks!

Srini

Best Answer

  • Ignacio_Toledo
    Ignacio_Toledo Dataiku DSS Core Designer, Dataiku DSS Core Concepts, Neuron 2020, Neuron, Registered, Dataiku Frontrunner Awards 2021 Finalist, Neuron 2021, Neuron 2022, Frontrunner 2022 Finalist, Frontrunner 2022 Winner, Dataiku Frontrunner Awards 2021 Participant, Frontrunner 2022 Participant, Neuron 2023 Posts: 411 Neuron
    edited July 17 Answer ✓
    Options

    Hi @Srini_E
    ,

    First, I had to remove some extra ',' (commas) when setting the variables, as in version 11.0.3 it complained when there was a comma at the end of the last element withing a {} group.

    Second, you are right, one you get the variable with:

    dataiku.get_custom_variables()['set1']

    What is returned is not a dictionary, but a string.

    I found a workaround

    import ast
    ast.literal_eval(dataiku.get_custom_variables()['set1'])['input_file']

    This will transform the string into a dict, and you can extract the subvariables. In this case I've got 'test/input.xlsx'

    Is not the cleanest solution, but it is a valid workaround. Maybe this should be requested as a new feature?

    Hope this helps

Answers

Setup Info
    Tags
      Help me…