We help IT Professionals succeed at work.

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

Jhovyn Marcos
on
212 Views
Last Modified: 2017-04-03
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
Comment
Watch Question

Brian TaoOwner
CERTIFIED EXPERT
Top Expert 2014

Commented:
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

Author

Commented:
@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"
Brian TaoOwner
CERTIFIED EXPERT
Top Expert 2014

Commented:
Then please attach or post the content of trelloApi.json

Author

Commented:
@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 "
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Julian and Brian: If you want to run this to ground, have a look here:
https://developers.trello.com/

Author

Commented:
@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.

:(
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.

Author

Commented:
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

Author

Commented:
Hope you could help me guys I really need this :( I'm still new in PHP Programming
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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

Author

Commented:
@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!
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.

Author

Commented:
ahhh..In firefox I got that error but in my google chrome that error is not appearing..Its weird..

Author

Commented:
@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!
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
@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.

Author

Commented:
@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!
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
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.

Author

Commented:
@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

Author

Commented:
@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..
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
And do you want to insert ALL the actions into that database.

Author

Commented:
@Julian Hansen..Yes sir if that is possible..

Author

Commented:
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!

Author

Commented:
Thank you sir for your great time
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
You are most welcome.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.