Parsing a Twitter's JSON representation

by javier (modified: 2017 Jul 03)

Parsing a Twitter's JSON representation (Part 2)

In the previous tutorial, we saw how to verify our credentials, because every request sent to Twitter's API must be authorized.

In this example we will parse the response in JSON format, we will use the JSON library. To learn more about JSON library check the following link JSON library

Resource URL

https://api.twitter.com/1.1/account/verify_credentials.json

Returns an HTTP 200 OK response code and a representation of the requesting user if authentication was successful; returns a 401 status code and an error message if not. Use this method to test if supplied user credentials are valid.

Check documentation for details here: Verify Credentials

Example

In this example, we will parse the JSON response and we will create a new object user instance of TWITTER_USER class. First, we will check that the Status code is 200, so we know everything is Ok and we have a response body, in another case we show the response status code.

The following snippet code shows how we do that after we got the Twitter's response, the l_body variable have the JSON response from twitter. Finally, we print out the current user's details and other useful information.

if l_response.status = 200 and then attached l_response.body as l_body then if attached {TWITTER_USER} (create {TWITTER_JSON}).verify_credentials (l_body) as l_user then print (l_user.full_out) if attached l_user.status as l_status then print (l_status.full_out) end else print ("%N Reponse: could not parse the response:" + l_body) end else print ("%NResponse: STATUS:" + l_response.status.out) end

Let's see how this work in details. The class TWITTER_JSON is responsible for parsing the JSON input and generate the corresponding Twitter object, in this case, a user, calling the feature verify_credentials.

verify_credentials (a_string: STRING): detachable TWITTER_USER -- Returns a representation of the requesting user if authentication was successful. local err: DEVELOPER_EXCEPTION do if attached parsed_json (a_string) as j then if attached string_value_from_json (j, "error") as l_error then create err err.set_description (l_error) err.raise elseif attached {JSON_ARRAY} json_value (j, "errors") as l_array then create err if attached string_value_from_json (l_array.i_th (1), "message") as l_err_message then err.set_description (l_err_message) end err.raise else Result := twitter_user (Void, j) end else print (a_string) end end This feature will parse the input a_string, representing the Twitter's response, and if everything is ok, will return an object representing a TWITTER_USER using the feature twitter_user, that will generate an object user from a json representation.

If the feature {TWITTER_JSON}).verify_credentials (l_body) can't parse the body, it will raise an error.

twitter_user (a_user: detachable like twitter_user; a_json: JSON_VALUE): TWITTER_USER -- Fill 'a_user' from 'a_json' require a_json_attached: a_json /= Void do if a_user /= Void then Result := a_user else create Result end Result.set_id (integer_value_from_json (a_json, "id")) Result.set_created_at (string_value_from_json (a_json, "created_at")) Result.set_name (string_value_from_json (a_json, "name")) Result.set_screen_name (string_value_from_json (a_json, "screen_name")) if attached string_value_from_json (a_json, "location") as s then Result.set_location (stripslashes (s)) else Result.set_location (Void) end Result.set_description (string_value_from_json (a_json, "description")) Result.set_profile_image_url (string_value_from_json (a_json, "profile_image_url")) Result.set_url (string_value_from_json (a_json, "url")) Result.set_protected (boolean_value_from_json (a_json, "protected")) Result.set_followers_count (integer_value_from_json (a_json, "followers_count")) if attached {JSON_OBJECT} json_value (a_json, "status") as l_status then Result.set_status (twitter_status (Void, l_status)) end end

The following piece of code show how we parse a string and return a JSON_VALUE.

parsed_json (a_json_text: STRING): detachable JSON_VALUE -- Parse 'a_json_text' and rerutn a JSON_VALE or Void. local j: JSON_PARSER do create j.make_with_string (a_json_text) j.parse_content Result := j.parsed_json_value last_json := Result end

Code Example

Get the code from here: https://github.com/EiffelWebFramework/cypress

apis \twitter \twitter_tutorial \parse