Parsers - selecting last element in an array without specifying exact index?

hey, wondering if anyone has any decent ideas to solve this condundrum in CBN 

given the below json object to parse into additional.fields with keys being Unique instances of "match_key", creating repeated fields from duplicate keys, how do you conquer this in CBN? I've included a snippet of some python code I have created that gets the desired result 

{
    "data_objs": [
        {
            "match_key": "test3",
            "match_string": "string.com"
        },
        {
            "match_key": "test2",
            "match_string": "string.xyz"
        },
        {
            "match_key": "test1",
            "match_string": "string.tech"
        },
        {
            "match_key": "test1",
            "match_string": "string.tech"
        },
        {
            "match_key": "RuleName",
            "match_string": "string.tech"
        }
    ],
    "domain": "somewhere.com",
    "issue_org": "Let's Encrypt"
}

 
python code 


result_object_udm = {
    "metadata": {
    "event_timestamp": datetime.now().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
    "event_type": "GENERIC_EVENT",
    "product_name": "TEST_PRODUCT",
    },
    "additional": {
        "fields": [
        ]
    }
}
unique_matches = []
result_store = []
for rule in data1['matched_rules']:
    if rule['match_key'] not in unique_matches:
        unique_matches.append(rule['match_key'])
        result_object = {
                "key": rule['match_key'],
                "value": {
                    "list_value": {
                        "values": [
                            {
                                "string_value": rule['match_value']
                            }
                        ]
                    }
                }
            }
        result_store.append(result_object)
    else :
        result_store[-1]['value']['list_value']['values'].append({
            "string_value": rule['match_value']
        })
    

for result in result_store:
    result_object_udm['additional']['fields'].append(result)
# print(json.dumps(result_store, indent=4))

 

I've got as far as line 35 in CBN before becoming confused at how to track which keys have been seen before and thus need merging,  while I can confirm that a key is or is not unique im not sure how to access the last element of an array to update the list_value.values array with the corresponding value

 

any ideas of alternative methods also appreciated 

0 4 399
4 REPLIES 4