How to call Global variables in nested JSON?

Solved!
Srini_E
Level 3
How to call Global variables in nested JSON?

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

0 Kudos
1 Solution
Ignacio_Toledo

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

View solution in original post

2 Replies
Ignacio_Toledo

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

Srini_E
Level 3
Author

Thanks for the work-around solution @Ignacio_Toledo. Yes, this should be requested as a new feature.

Thanks!

Srini