Link to home
Start Free TrialLog in
Avatar of Jhovyn Marcos
Jhovyn Marcos

asked on

Php - How to get the value of json file using PHP?

I have a question regarding on json.. I tried to get an api from other site and downloaded it as json file..I've already decoded this file using this code below..

 $str = file_get_contents('../trelloApi.json');
$json = json_decode($str, true);
echo '<pre>' . print_r($json, true) . '</pre>'; 

Open in new window


From this image attached, how do I get or print the value of the [list] => Array, [board] => Array and [card] => Array?

Please help me :(
js.jpg
Avatar of Brian Tao
Brian Tao
Flag of Taiwan, Province of China image

echo $json[173]['data']['list']['name']; // this will print "In Progress"
echo $json[174]['data']['board']['name']; // this will print "Accounting"

Open in new window

Avatar of Jhovyn Marcos
Jhovyn Marcos

ASKER

@Brian Tao
Thank you sir for your great time.. I followed your code but im getting this error "Cannot use object of type stdClass as array"
Then please attach or post the content of trelloApi.json
@Brian Tao Thank you again sir for your reply..Pls refer to my attached file.. I'm now getting this error "Undefined offset: 173 in ","Undefined offset: 174 in "
Your "attached file" is a picture.  What Brian is asking for it the "real thing" so please attach the file.

You might want to learn about the SSCCE.  It's the right way to ask a question at E-E, or in any other professional forum.

If you're an experienced PHP programmer, you might find this article helpful.
https://www.experts-exchange.com/articles/22519/Understanding-JSON-in-PHP-and-JavaScript-Applications.html

If you're new to PHP and want to learn "the right way" this article can lead you in the right direction.
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html
following on from Ray's post - we need the actual file trelloApi.json - not a picture of a dump of the decoded contents.
Click the attach file link at the bottom of the comment window
Browse to the file
Click the upload button
Give it a description
That should do it.
Julian and Brian: If you want to run this to ground, have a look here:
https://developers.trello.com/
@Brian Tao,@Ray Paseur,@Julian Hansen
Sorry for the late reply guys..I cannot upload a json file here..The extension of the uploaded file is not in the list of accepted extensions.

:(
You can put the JSON file on a public-facing server and post a link, right?  Or you can put the JSON into a code snippet, like this:
{ "key": "value" }

Open in new window

Sorry for the late reply guys..I cannot upload a json file here..The extension of the uploaded file is not in the list of accepted extensions.
Rename it to a .txt file and then upload it.
Good day Sir @Brian Tao,@Ray Paseur,@Julian Hansen...Thank you for your great time!
This is my API json generated..
https://www.dropbox.com/s/k14wkb59gb53a7h/trelloAPI.json?dl=0

Thank you again for your help
Hope you could help me guys I really need this :( I'm still new in PHP Programming
That helps but it seems to suggest you don't really know what you want from this data. The board and card properties are children of the data property which in turn is a child of actions - and there are multiple actions in the result set. In addition the board and card properties are themselves objects. It is not possible from the detail you have provided to know what you are looking for - however this code will dump all the board and card objects for all actions showing the action index they belong to. The code assumes the JSON data is saved in a file - you can adjust the code to get it from your own source
<?php
$data = file_get_contents('t2256.json');

$json = json_decode($data);
foreach($json->actions as $k => $action) {
	echo "Board for action [{$k}]: <pre>" . print_r($action->data->board, true) . "</pre>";
	echo "Card for action [{$k}]: <pre>" . print_r($action->data->card, true) . "</pre>";
}

Open in new window

Working sample here
@Julian Hansen
Thank you for your help sir..I really appreciate your help! How about the ['list'] array sir? I tried to add and followed your code but im getting an error "Notice: Undefined property: stdClass::$list"
echo "List for action [{$k}]: <pre>" . print_r($action->data->list,TRUE) . "</pre>";

Open in new window




I don't know how am I gonna to thank you for your help sir!
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your code is correct assuming it is used in the context of the foreach loop I posted earlier. If you are trying to use that code outside of the foreach loop then it will give an error.
ahhh..In firefox I got that error but in my google chrome that error is not appearing..Its weird..
@Julian Hansen
I know this is to much to ask sir forgive me but how do I get the data of my Board,List and Card? Can I assign a variable from them? example
Board
$name = name
$id = id
List
$name = name
$id = id
Card
$name = name
$id = id

Thank you again sir for your help.
God Bless!
@jhovyn,

how do I get the data of my Board,List and Card
This tells me you don't quite understand the data - there are multiple instances of those values - actions is repeated 1000 times and they are not all the same. Some items have list others have listAfter and listBefore (this explains the error you were seeing).

You need to tell us what you want to do with this data.

I have shown you how to get to each instance of the items in the actions list that contains a board, card and list value - you can't get a single value out - you need to explain how you want to use this data and then we can help you on how to access it.
@Julian Hansen
Thank you Sir..Actually this is what I want to do.
1. I need to distinguish each instance of the items in the actions list which you already shown to me..(thanks to you sir!)
2. If possible I have to get all the list of actions that contains a Board,Card and List Value and echo it in an html table.
3. If possible I have to assign a variable from that list of actions so that I can use it for insertion to my database.


Thank you again sir for your help! You are really a great help to me!
If possible I have to get all the list of actions that contains a Board,Card and List Value and echo it in an html table.
How do you want to do this? Each of those values is an object with multiple properties you would need to access the child properties directly - accessing the parent will not display anything of value.
The code above will find all the items you want - you just need to add one more bit to make sure it has a list property
<?php

$data = file_get_contents('t2256.json');

$json = json_decode($data);
foreach($json->actions as $k => $action) {
	// This will make sure that only actions with list properties will be processed
	if (isset($action->data->list)) {
		echo "Board for action [{$k}]: <pre>" . print_r($action->data->board, true) . "</pre>";
		echo "Card for action [{$k}]: <pre>" . print_r($action->data->card, true) . "</pre>";
		echo "List for action [{$k}]: <pre>" . print_r($action->data->list, true) . "</pre>";
	}
}

Open in new window


If possible I have to assign a variable from that list of actions so that I can use it for insertion to my database.
Again there are multiple instances - each of which is an object - which value do you need to get to inert into your db - can you show us your DB structure.
Just a thought... You might get a faster solution if you get the hands-on assistance of an E-E professional through E-E Gigs.
@Julian Hansen Good Day Sir!
which value do you need to get to inert into your db - can you show us your DB structure.

This is my table structure that will insert my api data...Pls. see attached file


Thank you again sir and God Bless you!
tbTrello.jpg
@Ray Paseur..Thank you for your advice sir but I know this is an awkward but to be honest I cannot afford to do it.. Please understand me sir..
And do you want to insert ALL the actions into that database.
@Julian Hansen..Yes sir if that is possible..
First of all thank you guys for helping me especially to Sir @Julian Hansen....I've already resolved this problem with the help of Sir @Julian Hansen..Expert Exchange is much better than the other forums..In other forums they only criticized and insult you on how do you program which is not helping..Again thank you guys :)

God Bless you!
Thank you sir for your great time
You are most welcome.