3

I have a big doubt about the JSON format the Services module (for Drupal 6) generates.

When I view the XML, all is OK.

<result is_array="true">
 <item>
  <nid>9</nid>
  <vid>9</vid>
  <type>producto</type>
 </item>
</result>

When I view the JSON, I see this:

[
 {
  "nid": "9",
  "vid": "9",
  "type": "producto",
 }
]

What I really is something like this:

[
 {
  "item":{
    "nid": "9",
    "vid": "9",
    "type": "producto",
  }
 }
]

How do I do this? Is there any configuration in the Services module that I can use to change the output, or do I need writing code?
I know the function to render JSON output.

  • Apologies I added the wrong link, this is the one you want: drupal.stackexchange.com/questions/20675/… – Clive Jan 26 '12 at 17:46
  • thanks, but i don't understand... I obtain the json file from services module. But have a different structure that i need... I this example, i view that i create a new data and encode this. I need to override this existing json file that is generated. – Eduardo Gabriel Cabrera Japa Jan 26 '12 at 17:55
  • You can use exactly the same method as in the linked answer, you override the abstract render() function in your class to change the JSON outputted by the module. You have access to the data object in $this->model – Clive Jan 26 '12 at 18:42
3

You should implement hook_services_request_postprocess_alter().

  • +1 can't believe I spent half an hour dissecting the RESTServer module to work out how to alter it's output directly when there's an overall hook. Doh :) – Clive Jan 26 '12 at 18:44
  • Can you provide a link to the documentation of the hook, or example code showing how the hook should be written? – kiamlaluno Feb 8 '12 at 8:43
  • Another example of the abysmal Services docs. Will work on D8 WSCCI kill any chances for anyone to take on that task? Stay tuned... :-) – paul-m Oct 10 '12 at 22:10
  • Work on D8 WSCII wil not kill services. – Kyle Browning Oct 10 '12 at 22:15
  • 2
    Tantalizing clues in services_log: drupalcode.org/project/services_log.git/blob/… This hook remains the only example Google can find. I really wish drupalcode.org were as easy to search as api.drupal.org. – paul-m Oct 10 '12 at 22:25
0

As I mentioned before, hook_services_request_postprocess_alter will not provide you what type of data it's processing. It means that your code must be really hacky to catch exactly type of data you need.

You better create your own formatter with hook_rest_server_response_formatters_alter, and use it to render data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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