Source "mapping.json":
{
"result": {
"src_color": "test_rule_2"
},
"rules": {
"color_degree": {
"test_rule_1": {
"color": 1
},
"test_rule_2": {
"color": 2
}
}
}
}
So it works perfectly:
with open("mapping.json", 'r') as json_file:
mapping = json.load(json_file)
expression = parse('$.rules.color_degree.test_rule_2.color')
match = expression.find(mapping)
if match:
pprint(match[0].value)
but in the path "test_rule_2" I need to replace with the value from result->src_color
How to properly describe something like this:
expression = parse('$.rules.color_degree.($.result.src_color.value).color')
If I understand your question correctly, it can be done, but you need two steps:
#first: get the variable:
src_expression = parse('$.result.src_color')
src_match = src_expression.find(mapping)
#second: get the target
expression = parse(f'$.rules.color_degree.{src_match[0].value}.color')
match = expression.find(mapping)
if match:
print(match[0].value)
Output is 2
.
EDIT:
I don't know why someone would want to do it in one step, but it's possible:
parse(f'$.rules.color_degree.{parse("$.result.src_color").find(mapping)[0].value}.color').find(mapping)[0].value
Same output.