in our app we have a Json file that contains several properties used for menu configuration. One of the keys should tell us if a menu item should be displayed. The thing is that the value for this key, can be an int but written as String "0" or "1", a Boolean written as String "true", "false", or an URL. So depending on the item, one of these 3 'magic' values tell us if a specific item should be displayed or not.

My question is, what is the best way or the elegant way of knowing the type of this value? We need to know the type for different handling.

At first I created a method for checking if the value is of Boolean type.

return value.equals(true.toString()) || value.equals(false. toString()));

The same, I should create a method if it is int, but for checking if it's a URL is a bit tricky.

So because I didn't find this approach very elegant and readable, I decided to add the type of this 'magic' key in the Json file: For Boolean type I added:

"myKeyType": "boolean"

And so on for the other 2. So I would like to know if adding a type property to Json file is a good idea, if it's Json responsibility to tell us the type, or is there a better way to do this?

EDIT: I can't change the type of data I receive, so I have to deal with the scenario I described. In this case, which of the 2 solutions I tried is more elegant? The first one, or the 2nd one with the extra key?


  • 1
    1) Having "0"/"1" alongside true/false seems completely redundant. Can you simply remove some of those values? 2) I thought figuring out types was normally the job of the JSON parsing/deserializing library. Or are you doing schemaless parsing so you get Objects and have to do all the type switching at runtime? 3) Why the strings "true"/"false" instead of just true/false?
    – Ixrec
    Commented Apr 2, 2016 at 3:13
  • I cannot remove int values nor change the fact they come as strings. This is how we receive them in another file (some config file). For "0"/" 1" values, In fact they tell the position of the item. So if 0, it means the item should not be displayed, if > 0, it should be displayed. Also for URL type, if there is an URL the item should be displayed, if empty it should not. I cannot change anything so I have to find a way to handle this with the kind of data I described.
    – Laura
    Commented Apr 2, 2016 at 3:27
  • Since you suggested adding a "magic key" to the JSON file, that implies you have some control over the contents of the JSON objects. What exactly are you allowed and not allowed to change?
    – Ixrec
    Commented Apr 2, 2016 at 3:31
  • 1
    To be more explicit, we have a config file that I am not allowed to change. In this config file we have keys and values for global configuration. Then we have a Json file, which we are allowed to change as it is part of our implementation. And in this Json file I have to write the key (not the value) we receive in the config file. In our code we get the value from this Json file, based on the key. So the only things I can change are: either I add new properties to that Json file which is more like a helper for that config file, either I handle this in Java code.
    – Laura
    Commented Apr 2, 2016 at 3:40
  • You want to use a JSON schema. Commented Apr 2, 2016 at 13:25

3 Answers 3


Sorry to say there is no elegant way of this messy design.

If you had some control it would be better to split these magic values into a plain array where each propertiy is of one type.

regarding your suggestion of "mykeytype":"boolean" some json libraries support this out of the box. Example json.net you can set typenamehandling=auto and it will add an extra field with type info when ever runtime type does not match static type.

I'm unavare if any libraries for Android support polymorphism but this question may be a place to start:

  • I understand. But, from those 2 bad solutions I mentioned, which is the least bad?
    – Laura
    Commented Apr 2, 2016 at 7:49
  • Am I correct in thinking you only have three values you need to consider. Maybe the best solution is simply a switch statement. Commented Apr 2, 2016 at 11:34
  • Basically there are 3 possible ways of checking if a menu item should be displayed. They are defined and cannot be changed in the configuration file as keys and value(e.g. "MyProfileEnabled" = "true", or " FacebookUrl" = "www.Facebook.com" where in this case because there is an URL set it means it should be displayed in the menu list, or "Settings"="1" where in this case 1 means it should be displayed on position 1 in the list. If it was 0 it wouldn't be displayed. And in the Json file I would use the key from config file, with which I would get its value in code.
    – Laura
    Commented Apr 2, 2016 at 16:31

You can really have anything you want in the JSON response as long as you define it as a standard and adhere to it. Naturally, you then need to parse the standardized JSON response in your code according to the presented rules.

However, I don't like the current design and would have redesigned the JSON response (from your recent comment it seems you have control over the structure).

The format could be

    "shouldBeDisplayed": true,
    "url": "http://myurl.com"

for a truthy case and either of the following formats when it should not be displayed:

    "shouldBeDisplayed": false,
    "url": null

    "shouldBeDisplayed": false

For a boolean variable, it would be nice if you returned nothing but true and false, even though some values may be considered truthy and falsy.


Since you mention you have control over the json with Java (in other words at the backend). I always favor shielding a front-end of these sort of proprietary concerns. Let Java create a json file that will fit like a glove, normalize the data before sending it across the wire.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.