JSON filtering with rest2mobile

Most REST APIs transmit data in a JSON format. JSON is simple, easy to read, and very flexible. Many times, however, you only care about extracting a fraction of the information found the JSON payload response.

For instance, I often use the google distance api example to explain how rest2mobile works.The example is in fact built out of a real cURL request:

[gist id=”9489ec86fdf997225fb9″ /]

If you use this example in rest2mobile, you will generate an object model that gives you access to every single entry in this JSON, when in fact, you only care about the distance value.

So instead you can modify the rest example as follows (only if you are using the command-line tool to generate it, otherwise you only need to copy-paste the request and response to the IDE plugin).

[gist id=”bdf5c7d373849050a7fc” /]

Doing so, the rest2mobile marshaller will simply ignore all the JSON entries that are not declared in your example (in this case, destination_addresses, origin_addresses, status, value, etc…).

This technic allows you to filter all the information that is not needed by your mobile app, reducing the size of your generated code, and making it easier to use.