How to read JSON Data and render it using theme_table function?

Events happening in the community are now at Drupal community events on www.drupal.org.
njmahesh's picture

I don't want to store the data in Drupal. I need to read JSON data from external URL and render that in my theme.

I could use Feeds and JSONPath Parser module if I want to store in Drupal but I just want to read. I am also tying Services module.

Any suggestions, tips would be huge help!

Thanks in advance

Comments

I will...

irishgringo's picture

be having this exact same issue pretty soon. I am pulling data from a CouchDB system. I have not given it much thought, but I am thinking something jquery-ish on the client end.

One other idea I had...

irishgringo's picture

I was also thinking of using node.js to make calls to the remote data. Then url calls from a field in DRUPAL. Of cource you could also write a module that would pull the remote data and do something with it.

One other idea I had...

irishgringo's picture

I was also thinking of using node.js to make calls to the remote data. Then url calls from a field in DRUPAL. Of cource you could also write a module that would pull the remote data and do something with it.

jQuery

agalligani's picture

What is it for? Can you just use jQuery or do you need the information on the backend?

The biggest issue I see for

alexiswatson's picture

The biggest issue I see for doing something on the client end is that it doesn't appear to be client-side functionality. What happens if they have it disabled, or a JS error somewhere else causes it to choke?

There are a few questions I would ask: Is it data that would ever be valuable to have represented as a collection of entities of some kind? Are the results cacheable? Does it need to be themed in more than one way (think teaser, full, etc.)? Why can't it be stored in Drupal itself?

I would still recommend considering Feeds and Feeds JSONPath Parser. We do this on the EMPAC site to retrieve information about events in some instances. Those results are saved as nodes and updated on the fly every so often, meaning we can later use those same results in Views, and so on. Feeds Tamper may also come in handy if you need to do some minor modification of the data before it is saved.

Small Request

phpsharma's picture

Hi David,

I need Help on how to use feeds jsonpath parser. can you show me the direction?like a step by step tutorial or video

Thanks in advance,
sharma

A lot of assumptions have to

ericthelast's picture

A lot of assumptions have to be made about what the data is and how you intend to use it but I'll give a scenario and explain how I would solve this problem.

Lets say there was a service that had a json feed of their upcoming events that was updated regularly. Lets also say that I just want to show a table in a block on a page. If I didn't care about caching (although block cache might help out a bit) and just wanted to "peek in" on what events they had coming up and show my visitors, I'd do the following:

-Create a custom module and create a block (hook_block_info(), hook_block_view())
-For the content of the block:
--I'd create a function that would use php's file_get_contents() to call the url providing the data
--Use json_decode to get an array of data
--Create a render array for the table (array('#theme' => 'table', '#header' => array(), '#rows' => array())
--Iterate through the results and populate the rows in the table
--Return the render array

Obviously there's some error checking and other things such as limits, storing the url in settings, etc that you could add but this is a basic outline of how I'd approach this issue.

Hope that helps.

Hello, Thanks a lot for the

selase's picture

Hello,
Thanks a lot for the scenario you've describe above. I have been trying to achieve a similar thing. Being relatively new to drupal, i picked up a pdf on module development but so far i haven't been able to do exactly what i want to do but i am gaining a better understanding of how modules work. I am trying to is similar to what can be found on this page http://ushik.ahrq.gov/. My drupal site would be making calls to an api that would return results in json format. And i will have to theme the results and display them as tables which can be sorted and searched. Infact every activity or request on the site would make api calls and return results. At this moment the focus is solely on display of the results. I sincerely appreciate the guidelines you have given above but i will like to ask if you could break them down or perhaps explain them a bit further. Otherwise do you have a tutorial or an example of how you would implement this, something we could learn from. Thank you.

I'd be more than happy to

ericthelast's picture

I'd be more than happy to give more details on how the module would look but you brought up two requirements that might change things a bit. The ability to search and sort on the data would probably work much better if you "crawl" the data sources and store the data locally...unless the api's that you will be calling give you the option to pass in parameters such as sort order, searching, paging, etc.

If you determine that you will need to crawl the data and store it locally then that's a pretty advanced requirement (although Feeds module might help a bit). If not, let me know and I'll draw up an example of what you need to get started.

Hello Eric, Thanks a lot and

selase's picture

Hello Eric,
Thanks a lot and good to hear from you. Searching, sorting and everything that would be requested would be sent as parameters to the api. I do not intend to store any information locally. I will be waiting to hear from you.

Thanks again.

hi erix

kaizerking's picture

nice explanation , above
I also have similar requirement here is the scenario:
I am using location module and geofield modules, i am getting it from http://services.gisgraphy.com/ajaxfulltextsearch.html.by using the URL in geocoder as explained here http://www.gisgraphy.com/free-access.htm .i am getting points i.e lat and long, the query also returns other relevant data.My whole purpose is to populate a country region city fields based on the results. the return format we have choice to pick up i.e xml,json,php http://www.gisgraphy.com/documentation/user-guide.htm#fulltextservice. the full text query results contain multiple instances of the given text. i want pop up a block with each instance and provide a check box where user can select the relevant result and click copy, which will copy to fields country, region/state.city,zip code fields.guidance needed pl

hi eric

kaizerking's picture

nice explanation , above
I also have similar requirement here is the scenario:
I am using location module and geofield modules, i am getting it from http://services.gisgraphy.com/ajaxfulltextsearch.html.by using the URL in geocoder as explained here http://www.gisgraphy.com/free-access.htm .i am getting points i.e lat and long, the query also returns other relevant data.My whole purpose is to populate a country region city fields based on the results. the return format we have choice to pick up i.e xml,json,php http://www.gisgraphy.com/documentation/user-guide.htm#fulltextservice. the full text query results contain multiple instances of the given text. i want pop up a block with each instance and provide a check box where user can select the relevant result and click copy, which will copy to fields country, region/state.city,zip code fields.guidance needed pl

Example just created as wiki page

shad_khan's picture

http://groups.drupal.org/node/267118
I am not connecting any json server in this example at that place using external database, that can be done easily just add one function with json call and pass that array to form as in example.

this example is for display the result in Sortable paginated table with form checkboxes,

Please suggest me to make this example more better. or where should I post proper examples for this complete module.

Thanks
Shad

shad_khan's picture

http://ushik.ahrq.gov/Search

Same module I have developed below are my steps.

Top Search Form - in block using block hook and create the form.
Main result page as form with one field checkbox
function my_module_page_form($form, &$form_state)

Theme the form
function theme_my_module_page_form($variables)

in this theme form I created drupal table
and added all form element using drupal_render in rows
and pass the header and rows arrays in theme_table function
$output = theme_table(
array(
"header" => $header,
"rows" => $rows,
"attributes" => array(),
"sticky" => true, // Table header will be sticky
"caption" => "",
"colgroups" => array(),
"empty" => t("Table has no row!") // The message to be displayed if table is empty
)
).theme("pager");

Please let me know if need any thing in more details
I can provide you complete code example if you want.

Thanks
Shad

Dear Shad, I am responding to

selase's picture

Dear Shad,

I am responding to both of your comments here. I am glad you wrote back.
1. I will be connecting to a server, through the api call and the results will come in json and so your example may take that into consideration.
2. i think the wiki is fine otherwise you can also comment the code in your text editor and zip it up for download. I will post it back in the wiki after working on it so others can learn from it.

Thanks and l look forward to hearing from you.

Selase

Yes I would like to see a

talthompson's picture

Yes I would like to see a full working code example if possible. thanks in advance.

i've just started to look

talthompson's picture

i've just started to look into this functionality as well. What is the latest on this project and has there been a module created?