Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How does one use JSON data within a template?


Go to End


6 Posts   2628 Views

Avatar
Sh33pDawgShep

Community Member, 10 Posts

17 February 2016 at 1:37am

Hi

Sorry, this might have been answered somewhere already but it's getting late and I'm finding myself running around in circles. I have JSON formatted data from a secondary source. I've come to:

json_decode($jsonData, true)

but for the life of me a I cannot see what to do next to get that data usable.

Please be gentle and go slowly; I'm trying to learn the system and so many things at the same time.

Thank you in advance.

Avatar
Devlin

Community Member, 344 Posts

17 February 2016 at 2:31am

Edited: 17/02/2016 3:36am

You need to convert the array to a ViewableData object (e.g. ArrayData/ArrayList)

https://docs.silverstripe.org/en/3.2/developer_guides/templates/rendering_templates/

A list:

// php
public function MyList() {
	return ArrayList::create(array(
		array('Foo' => 'Bar1'),
		array('Foo' => 'Bar2'),
	));
}

// template
<% loop $MyList %>
$Foo
<% end_loop %>

A single item:

// php
public function MyItem() {
	return ArrayData::create(array(
		'Foo' => 'Bar3',
	));
}

// template
<% with $MyItem %>
$Foo
<% end_with %>

I don't know what your json looks like -- and you should really check the data you're receiving -- but this should work as well:

public function MyList() {
	$array = json_decode($jsonData, true);
	return ArrayList::create($array);
}

Avatar
Sh33pDawgShep

Community Member, 10 Posts

18 February 2016 at 8:04pm

Edited: 18/02/2016 8:05pm

Thank you for your reply.

The way I ended up with usable data for my template was as follows:

            $prods=(json_decode($body,true));
            $result = new ArrayList ();
            foreach ($prods as $key => $prod) {
                if ($key == 'Products'){
                    foreach ($prod as $pro){
                        $result->push($pro);
                    }
                }
            }
            return $result;

This is for a JSON response from a remote server, using the RestfulService class, returning information about products from a cloud based inventory management software. The first couple of arrays in the response concern the number of responses; hence the conditional line if ($key == 'Products') to get down to a bunch of arrays with the information I need.

My question now is how should I better implement this; in light of this information and in context with your example?

Again, thank you for your time, it is appreciated.

Avatar
Devlin

Community Member, 344 Posts

18 February 2016 at 9:47pm

Looks ok to me. If you want to be on the safe side, then you should consider following coding rule #1: "All input is evil until proven otherwise."

That means, that you should validate the data before you accept the data with "$result->push($pro);"
... e.g.: Does $pro really contain all the data you expect? Is $pro['id'] really an integer? Does it not contain XSS code? etc. pp.

Avatar
Devlin

Community Member, 344 Posts

18 February 2016 at 10:00pm

Also, json_decode() does not return an array in case of an error. So you should check $prods before you iterate through the array.

$prods = json_decode($body, true);
$result = new ArrayList();
if (is_array($prods) && !empty($prods)) {
	foreach ($prods as $key => $prod) {
		// ...
	}
}
return $result;

Avatar
Sh33pDawgShep

Community Member, 10 Posts

19 February 2016 at 11:37am

Thank you. 'Real-world' examples such as this are great.