3

Is there a fool-proof method to generate a correctly formatted JSON data object to be received by a create node resource given by a REST server, defined with Services in Drupal 7?

I have setup a REST server accepting the application/json content type. I have also set up an anonymous endpoint with the create node resource enabled and I managed to create a new item with the Poster plugin for Firefox with Content Type set to application/json and the following data

{"title":"Review","type":"story"}

I can't seem to get any further though. I am following this guide and the node is created but my data for the rest of the fields is not added. I use this JSON editor to generate my code so it matches the structure shown by the dsm($_POST) call I placed in node.module's node_submit handler for testing.

data structure shown by dsm($_POST)

The JSON data I am trying to post looks like this (I remove all whitespace before using Poster):

{
"title":"great success!",
"type":"story",
"field_review":[
    {"und":
        [{"0":
            [
                {"value":"Some text"}
            ]
        }]
    }
],
"field_rating":[
    {"und":
        [
            {"rating":"60"}
        ]
    }
],
"field_product_type":[
    {"und": "1"}
],
"field_barcode":[
    {"und":
        [
            {"value":"33333333333"}
        ]
    }
]
}

What am I missing?

1 Answer 1

1

The function json_encode(); from my understanding.

4
  • Could you say a bit more? The function you refer to is a PHP function, if I understand it correctly, and I am trying to send data from an Android app (Java) to a Drupal REST server. Jun 1, 2012 at 19:43
  • 1
    drupal_json_encode() turned out to be helpful, actually. See this thread: drupal.org/node/1354202#comment-6456602 Sep 9, 2012 at 22:07
  • if the suggestion was helpful than select my answer and vote it up :) Sep 10, 2012 at 19:16
  • 1
    hi @benjamin_dk it would be really helpful if you could post some examples of how your json ended up looked for different field types. I'll follow the json_encode method at the link, but future visitors will really benefit. cheers Nov 26, 2012 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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