How to read JSON Fields

ieraora 03 Feb, 2025

Hi Max,

I would like to better understand how to read data in JSON with your new CF8 in Read Data see photo.

the data in the DB in this format: ["doc1.pdf","doc2.pdf","doc3.pdf"]

Best Regards

Cornelio

chrono-Screenshot 2025-02-03.png
Max_admin 03 Feb, 2025
Answer

Hi Cornelio

if you include the column in the JSON fields then it's value will be unpacked, in your case this will be an array

so if you just want to display it as a string (["doc1.pdf","doc2.pdf","doc3.pdf"]) then just remove it from the JSON fields

the JSON fields is helpful if you have a JSON object: {"key":"value"} so that you can use a Data Path like up_allegato.key to display "value" in the table column

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ieraora 03 Feb, 2025

HI Max, 

thanks for quickly replay.

if you include the column in the JSON fields then it's value will be unpacked, in your case this will be an array

I have an array ["doc1.pdf","doc2.pdf","doc3.pdf"] on the DB

so without resorting to PHP code you can explain to me how to use your CF8 to extract the data  with json

Max_admin 03 Feb, 2025

what do you want to do with the JSON value in that column ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
Elita- 04 Feb, 2025

Max, in CF6 there was a shortcode that would split the joinded multi values: 

{data.split[]:field_name} or {var.split[]:read_data.Model.field_name} where in [] you put the separator. 

how it is in CF8? 

ieraora 04 Feb, 2025

Hi Max,

I'll show you below with the following example what i would like to get

1) this is the content in the db in JSON format

{
    "ewrhbw": {
        "age": 32,
        "name": "Sara"
    },
    "kdsvhe": {
        "age": 42,
        "name": "Andrew"
    },
    "klkwcn": {
        "age": 17,
        "name": "Mina"
    },
    "lvnwfd": {
        "age": 25,
        "name": "Alex"
    }
}
<?php
//get identity
use Joomla\CMS\Factory;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Helper\ModuleHelper;
use Joomla\Session\SessionInterface;
use Joomla\CMS\Application\SiteApplication;

$container 	= Factory::getContainer();
$container->alias(SessionInterface::class, 'session.web.site');
$app 		= $container->get(SiteApplication::class);
$user 		= Factory::getUser();
echo '<p><br>User ' . $user->username . '</p>';
//echo '<p><br>UserID ' . $user->id . '</p>';
$userid = $user->id;
echo $userid;

//get DBdata

$db = Factory::getContainer()->get('DatabaseDriver');
$query = $db->getQuery(true);
$query->select(array('up_allegato','up_user_id'));

$query->from($db->quoteName('#__cf8_job_upload'));

$db->setQuery($query);
$result = $db->loadResult();

$decoded_json = json_decode($result , true);

foreach($decoded_json as $key => $value) {
    $name = $decoded_json[$key]["name"];
    $age = $decoded_json[$key]["age"];
    
    echo $name.' is '.$age.' years old.<br>';
}

2) this is the result after running the query

Sara is 32 years old.

Andrew is 42 years old.

Alex is 25 years old.

Mina is 17 years old.

my question is: 

How can I get the same result with CF8

Thanks a lot

Max_admin 04 Feb, 2025
1 Likes

@Elita, there is no split function in v8 because the recommendation is to store values in JSON instead of joining them with a comma, but there is a .encode and .decode functions

@ieraora, is this the content of one column ? how do you want to list these values ? do you want them listed under one table cell ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ieraora 05 Feb, 2025

Hi Max, thank you  for replay

@ieraora, is this the content of one column ? 

Yes, on a column.

how do you want to list these values ? 

Yes

do you want them listed under one table cell ?

each extracted data will have a link so better on a column, see 'echo below and  also attached

foreach($decoded_json as $key => $value) {
    $name = $decoded_json[$key]["name"];
    $age= $decoded_json[$key]["age"];
      
     echo '<div class=" area_container18"><table class="nui table white celled selectable bordered rounded full width"><tr style="border:1"><td><a href="' . $url  . $age . '">' . $name .'</a></td><tr></table></div>';
    
}
Screenshot 2025-02-05 alle 09.47.47.png
Max_admin 05 Feb, 2025

The simplest way is to use your own PHP code in a PHP code under the column blue section:

$decoded_json = json_decode($this->get("row.column_name") , true);

foreach($decoded_json as $key => $value) {
    $name = $decoded_json[$key]["name"];
    $age = $decoded_json[$key]["age"];
    
    echo $name.' is '.$age.' years old.<br>';
}

it will look like this:

How to read JSON Fields image 1

this assumes that you did NOT add the column to the JSON fields, if you do then the first line will not be needed

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
ieraora 06 Feb, 2025

Hi Max, 

thanks a lot for your replay but,

unfortunately your suggestion does not solve the problem,

where you say: his assumes that you did NOT add the column to the JSON fields, if you do then the first line will not be needed

the column seems to be necessary,

my initial request was to understand how to avoid using custom php code by using your CF8 Read Data see photo3 instead of Table9 see photo

photo3.png
ieraora 06 Feb, 2025

according to your suggestion see below

photo1.png
ieraora 06 Feb, 2025

according to your suggestion see below

photo2.png
Max_admin 06 Feb, 2025

what is the output of the code I provided using the PHP action in the Table cell as in the screenshots ? you have the PHP outside the table

you may just use the code I provided, do not worry about adding the field to the JSON fields setting

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.

VPS & Email Hosting 20% discount
hostinger