Question
I receive a JSON list of nested arrays with data:
[
{
"firstLevel_1": "some_shit_value",
"firstLevel_2": {
"secondLevel_1": "value_1",
"secondLevel_2": {
"id": 10,
"value": "gold_11"
}
},
"Date": "value_21"
},
{
"firstLevel_1": "some_shit_value",
"firstLevel_2": {
"secondLevel_1": "value_1",
"secondLevel_2": {
"id": 11,
"value": "gold_11"
}
},
"Date": "value_22"
},
{
"firstLevel_1": "some_shit_value",
"firstLevel_2": {
"secondLevel_1": "value_1",
"secondLevel_2": {
"id": 12,
"value": "gold_11"
}
},
"Date": "value_23"
}
]
I need to extract one Date value if we know the value in the id key.
Answer
If we are searching for Date by id
in this example, it will be like this:
=: #json_field[$row_num][Date]
row_num: $search_key_num[0]
search_key_num: listSearch(list: #json_field[[firstLevel_2]][[secondLevel_2]][[id]]; key: "value" = 11)
This code in the Totum system is used to retrieve a value from the json_field
in the current table if the nested key in this field equals a specific value.
In this case, it searches for the value of the Date key if the nested key id equals 11.
#json_field[$row_num][Date]
: The main line of code that returns the value of the Date key from the json_field in the current table. The row index is determined by the row_num line of code.
row_num
: The line of code that determines the row number where the match is found. In this case, it is the first element of the search_key_num list.
search_key_num
: A list that contains the row numbers where a match is found for the key id with the value 11.
listSearch
: A function that searches all keys in the json_field list and returns their numbers where the value of the key id equals 11.
#json_field[[firstLevel_2]][[secondLevel_2]][[id]]
: An indication of the nested key id in the json_field at the third level, first firstLevel_2, then secondLevel_2, and then id.
key: "value" = 11
: The condition by which the match is searched. In this case, it is the value 11.
As a result, we get the value of the Date key from the json_field if the nested key id equals 11.