I have the attached following actions parameter part of my JSON data.
'actions': [{'action_type': 'onsite_conversion.post_save', 'value': '1'},
{'action_type': 'link_click', 'value': '2'},
{'action_type': 'post', 'value': '3'},
{'action_type': 'post_reaction', 'value': '4'},
{'action_type': 'video_view', 'value': '5'},
{'action_type': 'post_engagement', 'value': '6'},
{'action_type': 'page_engagement', 'value': '7'}],
API can send the following options at any time;
action_types = ["onsite_conversion.post_save", "link_click", "post", "post_reaction", "comment", "video_view", "post_engagement", "page_engagement"]
I tried to write a python script that parses these possible values (in action_types list order) from the JSON body, as seen from the sample JSON data it doesn’t send comment value so in this case script should write 0 to return list, below is my script
def get_action_values(insight):
action_types = ["onsite_conversion.post_save", "link_click", "post", "post_reaction",
"comment", "video_view", "post_engagement", "page_engagement"]
action_type_values = []
for action in action_types:
if action in [item["action_type"] for item in insight["actions"]]:
for item in insight["actions"]:
if action in item["action_type"]:
if "value" in item:
action_type_values.append(item["value"])
else:
action_type_values.append("Null")
else:
action_type_values.append(0)
return action_type_values
I am expecting it to return as [1,2,3,4,0,5,6,7]
but it returned as [1, 2, 1, 3, 4, 6, 4, 0, 5, 6, 7]
2
Answers
As you can see in the code you posted and as it was already pointed out in the comments to your question:
'value'
has a value of'2'
not2
, so if you expect integers in the resulting list use:or to be consistent with the string representation:
Another issue was caused by the if-condition
if action in item["action_type"]:
you use because you have an action type'post'
which occur in all of'onsite_conversion.post_save'
,'post'
,'post_reaction'
and'post_engagement'
also containing'post'
, so it is necessary to change the condition (as pointed out by barmar in the comments) to:if action == item["action_type"]:
to eliminate duplicates in the result.Below the entire corrected code:
printing the expected:
Animated by Jamiu S. answer which tries to optimize the code but does generally not work properly ( as of 2023-02-05 01:06 CET ) failing with KeyError or not printing all the values, below improved code eliminating one loop and using
dictionary.get(key, default_value)
syntax to eliminate anif/else
code section.The code includes changed input data to show that it covers cases in which the code in Jamiu S. answer fails to work or to work properly. Notice that the returned list items are all strings to be consistent with
'Null'
and the'action_type'
values:prints
Here is a possible solution:
The
action_type_values
list is initialized with zeros of the length ->len()
ofaction_types
which represents the default value for any missingaction_type
in the insight dictionary.The
for-loop
then loops through all the actions for each action, ifaction_type
is inaction_types
, it’s index is found and the corresponding value in theaction_type_values
is updated with theint(value)
of the action.