How to json_decode() object list

40 views
Skip to first unread message

Harold Smith

unread,
Jul 2, 2024, 7:29:59 PMJul 2
to Joomla! General Development
I have not been able to find this here so I may be asking again.  I have a simple object list I need to decode.  I never seem to remember how to work with the json_decode/_encode data.
Thanks in advance.

Array ( [0] =>
stdClass Object (
 [version] =>
 ["J5","PHP 8.3"] ) [1] =>
 stdClass Object (
 [version] =>
["J3","J4","J5","PHP 8.2"] ) )

René Kreijveld

unread,
Jul 3, 2024, 7:09:50 AMJul 3
to Joomla! General Development
This seems already decoded. Your example above is not JSON.
json_decode decodes a JSON string to arrays.
json_encode goes the other way: encode an array to a JSON string.


Op woensdag 3 juli 2024 om 01:29:59 UTC+2 schreef haroldscot...@gmail.com:

Harold Smith

unread,
Jul 3, 2024, 9:23:46 AMJul 3
to joomla-de...@googlegroups.com
Hello

I'm sorry this isn't decoded It's data from my table that needs decoded.  Every foreach i have tried does not work for me because I am not sure how to get the data and decode it to a json string.

--
You received this message because you are subscribed to the Google Groups "Joomla! General Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to joomla-dev-gene...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/joomla-dev-general/b37b8acc-4401-4565-ae06-89cc92a9ac14n%40googlegroups.com.

Harold Smith

unread,
Jul 3, 2024, 4:27:40 PMJul 3
to Joomla! General Development
I will try to clarify. I started by trying to get the table data:

$db = Factory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('version'));
$query->from($db->quoteName('#__mytable_products'));
$query->where($db->quoteName('published')." = 1");
$db->setQuery($query);
$versions = $db->loadObjectList();

From here I have tried and tried a ton of foreach() statements with and with out a key for the statement and thought I was going to decode there.  Unfortunately I have objects and I do not know where/how to decode this to get my versions.

René Kreijveld

unread,
Jul 3, 2024, 5:12:57 PMJul 3
to Joomla! General Development
Harold,

If this is for Joomla 5 and up, I would write your code as follows:

use \Joomla\CMS\Factory;
use \Joomla\Database\ParameterType;
$db = Factory::getContainer()->get('DatabaseDriver');

$query = $db->getQuery(true)
->select($db->quoteName('version'))
->from($db->quoteName('#__mytable_products'))
  ->where($db->quoteName('published') . ' = :published')
  ->bind(':published', 1, ParameterType::INTEGER);

$db->setQuery($query);
$versions = $db->loadObjectList();

This code retrieves an array of objects stored into the array $versions. Each object in the array has one variable: version.
To list these, you can use a foreach loop:

foreach ($versions as $version) {
    echo $version->version;
}

There are also other ways to retrieve the data, you can read more about that here:
https://docs.joomla.org/Selecting_data_using_JDatabase

Op woensdag 3 juli 2024 om 22:27:40 UTC+2 schreef haroldscot...@gmail.com:

René Kreijveld

unread,
Jul 3, 2024, 5:20:36 PMJul 3
to Joomla! General Development
To retrieve the json encoded data, you could use the following code:

foreach ($versions as $v) {
    // Decode json version string
    $json_decoded_values = json_decode($v->version);
    // $json_decoded_values is now an array holding all values from the json string

    // list all values
    foreach ($json_decoded_values as $value) {
        echo $value . '<br>';
    }
}


Op woensdag 3 juli 2024 om 23:12:57 UTC+2 schreef René Kreijveld:

Harold Smith

unread,
Jul 3, 2024, 7:06:22 PMJul 3
to Joomla! General Development
Thank you very much.  This does what I needed it to do.  I spent hours on hours doing it all backwards.
Reply all
Reply to author
Forward
0 new messages