Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm stuck on nested arrrays within the JSON argument i.e.
I have set up a new type and added a TEXT CCK field - field_html_song_list
{"type":"rundata","uid":"1","title":"TITLE","body":"BODY","field_html_song_list":[{"value":"Song","format":"2"}]}
The above JSON just pulls up a blank page when running manually (/admin/build/services/browse/node.save)
and returns Missing Argument on DrupalCloud
However, this works fine and sets up a a new node:
{"type":"rundata","uid":"1","title":"TITLE","body":"BODY"}
Comment | File | Size | Author |
---|---|---|---|
#2 | 952344_cast_objects_in_args_to_array.patch | 312 bytes | voxpelli |
Comments
Comment #1
matteblacke CreditAttribution: matteblacke commentedAfter enabling php errors i get the below after running this JSON
{"type":"rundata","uid":"1","title":"TITLE","body":"BODY","field_html_song_list":[{"value":"Song","format":"2"}]}
Fatal error: Cannot use object of type stdClass as array in public_html/modules/cck/modules/text/text.module
Comment #2
voxpelli CreditAttribution: voxpelli commentedThis is an error in the JSON server module.
Services supports only associated arrays as arguments - not objects. The json_decode of the JSON below results in field_html_song_list containing an object instead of an array.
{"type":"rundata","uid":"1","title":"TITLE","body":"BODY","field_html_song_list":[{"value":"Song","format":"2"}]}
The reason this hasn't been discovered earlier is probably because Services itself does some basic type casting - but no recursive casting.
Attached is a patch that fixes this by specifying to json_decode() that we prefer arrays.
Comment #3
matteblacke CreditAttribution: matteblacke commentedI have applied the patch and it fixes the problem
thank you
Comment #4
spazefish CreditAttribution: spazefish commentedGreat and thanks for pointing me to this matt, awesome ! :D
Comment #5
gddDoes this break existing implementations of JSON Server? Or does it just fix the stuff that has always been broken?
Comment #6
voxpelli CreditAttribution: voxpelli commentedLooking at the Services 2.x browser and at issue #945788: Change node.save argument type to array I think my patch is not entirely correct (but it might work anyway?).
JSON Server should check if the argument type is a struct or an array and depending on that parse the JSON differently - my patch treats struct as array and right now arrays are treated like structs. A problem with doing a correct fix now is that until #945788: Change node.save argument type to array is fixed the node.save argument has the wrong type so the core issue here, that CCK fields can't be saved, can't get a correct fix until Services 2.x is fixed.
Also related to this issue is a suggestion for Services 3.x: #954964: Remove argument type 'struct' in favor of 'array' It's a suggestion to simplify the argument types in order to avoid such complexity and confusion for the servers that we see in this and similar issues.
Comment #7
kylebrowning CreditAttribution: kylebrowning commented#945788: Change node.save argument type to array
Was fixed and committed. I believe that solved this issue and if not, feel free to mark as needs work.
Comment #8
voxpelli CreditAttribution: voxpelli commentedJSON Server should still check if the argument type is a struct or an array and depending on that parse the JSON differently - I don't think #945788: Change node.save argument type to array fixed this since JSON Server never checks the type.
Comment #9
kylebrowning CreditAttribution: kylebrowning commentedMy apologies, for some reason I thought this was a duplicated ticket in the Services Issue Queue.
Comment #10
batje CreditAttribution: batje commentedI would not mind if this got committed. The patch in #2 works for me
Comment #11
mayur.pimple CreditAttribution: mayur.pimple commentedHow to pass cck argument in node.save drupal services
{"title":"title1","type":"poll","choice":[{"0"}]:[{"chtext":"1"}],"choice":[{"1"}]:[{"chtext":"2"}]}
Comment #12
elliotttt CreditAttribution: elliotttt commentedI see this patch worked for some people, but it's didn't work for me however the problem remained the same.
My interim hack-ish fix was to cast the node property as an array like so:
Any suggestions on how to recursively check for this in a more elegant way would be wonderful. I thought I'd post this anyway in case other people are still stuck on this.
Comment #13
elliotttt CreditAttribution: elliotttt commentedCame up with a better way to deal with this... I'm going to see what other repercussions it might have before creating a patch.
Added the above to the module, then added this to the json_server_server function: