Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a JSON array, and I want to pass it to drupal function to work with the data. what are the possible ways to do that?

any code sample would help

share|improve this question

1 Answer

up vote 3 down vote accepted

The function drupal_json_decode() (D7 only) can convert a json string to the corresponding php data structure.

In Drupal 6, you can directly use the PHP function json_decode(), just remember that you need PHP 5.2+ for that.

As I almost assumed, the part you're having problem with is not converting the JSON string to a PHP structure you can work with, but sending that from the client side to the server, so that you can access it with PHP (Try to provide more information what exactly you want to do when asking questions and you'll get better answers).

drupal_json_decode() is, in fact, just the very last step in the whole process. You obviously can't access that function (or anything else that is PHP) directly from jQuery, so you need to send it with jQuery.post() to the server, where it can be processed.

To be able to do that, you first need to define a endpoint where the data can be sent to by implementing hook_menu(), for example your_module/endpoint. In the page callback that you defined there, you can read the sent data in from php://input, for example with $json = file_get_contents("php://input");. Then, you can use the mentioned function to parse $json.

share|improve this answer
how to use drupal_json_decode from javascript or jquery code? – Firdous Oct 12 '11 at 17:46
Updated the answer. I suggest you ask a new question (if a search doesn't give you anything) if you have follow-up questions to the single steps. – Berdir Oct 12 '11 at 18:13
thanks it worked – Firdous Oct 13 '11 at 12:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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