1

If I send a JSON string to my controller that has a nested object in it, rather than just a simple object with a bunch of scalar properties on it, I don't seem to have a way of accessing the nested object effectively. If I 'get' the name of the nested object, I am returned an array of the property values of the nested object, which isn't much use to me. Any hints?

Example:

Sending this:

{"feedback":{"howhear":"Leaflet","comment":"Nice Joomla! site"}}

And accessing it like this:

$app = JFactory::getApplication();
$jinput = $app->input->json;
$feedback = $jinput->get('feedback');

Just gives me:

[ "Leaflet", "Nice Joomla! site" ]

Can I get the object instead using JInputJSON, or do I need to fallback to handling the raw POST data ie via php://input

EDIT:

To clarify, the key benefit I want from using JInputJSON is the filtering capabilities it provides. I know json_decode is available for the task, but it provides no filtering eg. to combat SQL injection attacks.

  • Can you not just json_decode the input in your controller and convert it to an array? – Terry Carter Jul 22 '16 at 1:22
1

How do you do requests?

I did something like the following in my projects:

$jinput = JFactory::getApplication()->input->json;
$feedback = $jinput->get('feedback', array(), 'array'); // will return your array with keys

To get nested items with filters: You can also nest arrays to get more complicated hierarchies of values:

$fooValues = $jinput->getArray(array(
    'feedback' => array(
        'comment' => 'string',
        'someint' => 'int'
    )
));

Source: https://docs.joomla.org/Retrieving_request_data_using_JInput

Additional info for others using JinputJson:

JInputJson Joomla Doc

JInput Class Source

JInputJson Class Soure

JinputJson just extends Jinput and changes $this->data to encoded JSON (as array). You can then retrieve the data with the get function.

I would recommend to build your JSON object like {"comment":"abc","howhear":"abc"}, so you could use the filter on the entries directly:

$feedback['comment'] = $jinput->get('comment', '', 'string');
  • This is just a more specific version of @ioctopus' answer. The problem is the same - there's no filtering with this solution. – John Rix Jul 27 '16 at 12:01
  • Please see my updated answer. – Dennis Heiden Jul 27 '16 at 13:56
  • I appreciate the feedback. Unfortunately, it defeats the point of structuring my inputs though. I have multiple groups of data points in my browser page that I am pulling together in the request, and flattening the data structure like that is not an option for me. – John Rix Jul 27 '16 at 14:46
  • See my update about nested arrays, can't check it right know. Otherwise using escaping functions of the db object in the outgoing process will also help against injections. – Dennis Heiden Jul 27 '16 at 17:34
  • 1
    @DennisHeiden If you don't mind, please update the hyperlinks in your answer -- the current urls are deadlinks. (aww geez, I don't have enough rep on this satellite for my edit to instantly publish -- I guess I'll need to build up some rep here.) – mickmackusa Mar 20 '18 at 23:44
0

As one of the comments stated, why not use json_decode?

$objJSon = json_decode($json); //if you want you can add true as a second 

You can then retrieve the values from $objJSon. Again, if you want an associative array, just change json_decode($json) to json_decode($json, true)

  • You could do that, yes, but I'd also like to gain from the filtering capabilities of JInput rather than having to do all that manually myself. (I know you can use JFilter directly also, and that's what I'll probably have to resort to.) – John Rix Jul 22 '16 at 22:00

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.