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.

3
  • Apologies I added the wrong link, this is the one you want: drupal.stackexchange.com/questions/20675/…
    – Clive
    Jan 26, 2012 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. Jan 26, 2012 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, 2012 at 18:42

2 Answers 2

3

You should implement hook_services_request_postprocess_alter().

6
  • +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, 2012 at 18:44
  • Can you provide a link to the documentation of the hook, or example code showing how the hook should be written?
    – apaderno
    Feb 8, 2012 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, 2012 at 22:10
  • 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, 2012 at 22:25
  • 1
    With that hook, there is no way to know what type of data is it.
    – mrded
    Sep 17, 2015 at 11:14
1

As I mentioned before, hook_services_request_postprocess_alter() won't provide you what type of data it's processing. It means that your code must be really hacky to catch exactly the 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.