Inconsistent data type for targeting_value in targeting criterion


Looking at the documentation for the methods that add targeting criteria to a line item, I’ve noticed that in some cases the targeting_value field is an integer value and in other cases, it is a string value. For example, on the PUT targeting criteria method the sample response at the bottom of the page shows that the criterion named “Male” has "targeting_value": 1 while the criterion named “snowboarding” has "targeting_value": "snowboarding".

A situation where a field of a Json object may have different data types depending on context creates a real inconvenience for developers using certain Json parsing libraries (e.g. Spray). Can Twitter please eliminate this inconsistency?


Hi @dodnert,

I don’t know the exact problem, but I think with your twitter library for your code language that should be transparent for the developer.

What kind of code language are you using?



Our application is written in Scala. We are using the Spray framework to make HTTP requests to Twitter APIs and to parse the Json objects in the response.


Hi @dodnert,

I’m not really familiar with that language, so I think i can’t help you, sorry.

Hopefully someone could help you.



Thank you for trying to help, @hector_borras. However, I don’t really consider this a Scala issue. I consider this to be a poor design decision in the Twitter Ads API. Inconsistency in the data type of a named field is a bad idea. There’s no reason why this field could not have consistently been a string value. Then, for those targeting types that expect an integer value, the string can easily be converted to an integer. The logic for how to interpret the targeting_value can live in the code that deals with the targeting criterion after it has been parsed from Json. This is a much better design approach than having to give the Json parser an awareness that a field could be either one of two data types.


I’ve found that I can work around the inconsistency by declaring the Scala data type of the field to be Either[String,Int]. The Spray Json parser handles this implicitly.

Nevertheless, I still consider the type inconsistency to be a poor API design choice.


Howdy @dodnert,

gender is actually a string as well, per the docs you linked. The only two targeting_values I could find that are integers are network_activation_duration_lt and network_activation_duration_gte.

I tend to agree with you. Overall, we have tried to make targeting_values strings, however those two seem to have slipped through. I’ll file a ticket but no promises on getting that changed in this revision of the API.



Thank you for the reply @c_r_w. In my testing of the Ads API, targeting criteria with the gender targeting type are returned with an integer targeting_value, not with a string.


Hi @dodnert,

You are right, I misunderstood your issue. I’ll file a separate ticket for this issue.