How do i access multiple nested json data - php
I am having issues accessing the nested data from this. I can access the first top level object but not the nested data. Any suggestions, Thnx
{"userInput":{"relaSource":"DailyMed","relas":"has_EPC","drugName":"intuniv"},"rxclassDrugInfoList":{"rxclassDrugInfo":[{"minConcept":{"rxcui":"40114","name":"Guanfacine","tty":"IN"},"rxclassMinConceptItem":{"classId":"N0000175554","className":"Central alpha-2 Adrenergic Agonist","classType":"EPC"},"rela":"has_EPC","relaSource":"DAILYMED"}]}}
my code is as follows (some functions are tests) but i need to access all datapoints..... relaSource, relas, drugName, rxclassDrugInfoList-> rxclassDrugInfo, minConcept, rxcui, name, tty,
<?php
$url = 'https://rxnav.nlm.nih.gov/REST/rxclass/class/byDrugName.json?drugName=intuniv&relaSource=DailyMed&relas=has_EPC'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
echo $data;
echo "<br/>";
$jsonObj = json_decode($data);
echo "<br/>" . $jsonObj->userInput->relaSource;
echo "<br/>" . $jsonObj->userInput->relas;
echo "<br/>" . $jsonObj->userInput->drugName;
echo "<br/>";
echo "A2";
?>
<?php
foreach ($jsonObj as $data) {
$relaSource = $data->relaSource;
$content = $data->relaSource;
?>
<h1> <?php echo $relaSource; ?></h1>
<h1> <?php echo $content; ?> </h1>
<?php } ?>
$rxclassDrugInfoList = $jsonObj['rxclassDrugInfoList'];
echo $rxclassDrugInfoList['rxclassDrugInfo']."<br/>";
/////////////////////////////////////////////////////////////////////////////
<h2>TEST</h2>
<?php
$url = 'https://rxnav.nlm.nih.gov/REST/rxclass/class/byDrugName.json?drugName=intuniv&relaSource=DailyMed&relas=has_EPC'; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
// JSON string
//$someJSON = '[{"name":"Jonathan Suh","gender":"male"},{"name":"William Philbin","gender":"male"},{"name":"Allison McKinnery","gender":"female"}]';
// Convert JSON string to Array
$someArray = json_decode($data, true);
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["userInput"]; // Access Array data
///////////////////////////////////////////////////////////////////////////////////////////
// Convert JSON string to Object
$someObject = json_decode($data);
print_r($someObject); // Dump all data of the Object
echo $someObject[0]->userInput; // Access Object data
// Convert JSON string to Array
$someArray = $someObject[0]->userInput;
print_r($someArray); // Dump all data of the Array
echo $someArray[0]["name"]; // Access Array data
echo "<br/>";
echo "<br/>";
echo "<br/>";
?>
I know it is just a problem with addressing items that are nested incorrectly.
**Here is all the datapoints that u needed **
<?php
$json = '{"userInput":{"relaSource":"DailyMed","relas":"has_EPC","drugName":"intuniv"},"rxclassDrugInfoList":{"rxclassDrugInfo":[{"minConcept":{"rxcui":"40114","name":"Guanfacine","tty":"IN"},"rxclassMinConceptItem":{"classId":"N0000175554","className":"Central alpha-2 Adrenergic Agonist","classType":"EPC"},"rela":"has_EPC","relaSource":"DAILYMED"}]}}';
var_dump($json);
var_dump(json_decode($json)->userInput);
var_dump(json_decode($json)->rxclassDrugInfoList);
var_dump(json_decode($json)->userInput->relaSource);
var_dump(json_decode($json)->userInput->relas);
var_dump(json_decode($json)->userInput->drugName);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->minConcept->rxcui);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->minConcept->name);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->minConcept->tty);
var_dump(json_decode($json)->rxclassDrugInfoList->rxclassDrugInfo[0]->rxclassMinConceptItem->classId);
?>
Related
PHP rest api display an array of values
I am trying to display all the items from an API. How can i display in html or store them in variables. In my code $api_url = 'demo API'; // Read JSON file $json_data = file_get_contents($api_url); // Decode JSON data into PHP array $response_data = json_decode($json_data, true); $result = array_values($response_data); var_dump($result); but unfortunately i dont know how to display the datas in front
i found a way <?php // JSON string $json_string = '(API)'; $jsondata = file_get_contents($json_string); $obj = json_decode($jsondata,true); foreach($obj as $row => $innerArray){ foreach($innerArray as $innerRow => $value){ // echo $innerRow.' = '.$value . "<br/>"; if ($innerRow == "thumb_url") { ?> <img src="<?php echo $value; ?>" width="280" height="360"> <?php } elseif ($innerRow == "title"){ ?> <?php echo $value; ?> <?php } } } ?>
how to echo fixer.io json php
Hi I'm trying get a json from fixer.io and then for each rates echo it but cant get it to work. the code are <?php function usd(){ echo 'HEJ test'; $fixer_access_key = my_access_key; $url= 'https://data.fixer.io/api/latest?access_key=' . $fixer_access_key; echo $url; $json = file_get_contents($url); $data = json_decode($json); echo $url . "<br>"; echo 'printing json foreach <br>'; foreach($data as $obj){ echo '...'; $prefix = $obj; echo $prefix; echo '<br>';} echo 'done printing json foreach'; } usd(); ?> and the result are: https://data.fixer.io/api/latest?access_key=my_fixer_key printing json foreach done printing json foreach
instead of $data = json_decode($json); use $data = json_decode($json, true); This should allow foreacha to works - however you will only see first level of json object keys (not nested ones). The second parameter of json_decode change result from object to array. You will also need to change foreach - to following: foreach($data as $key => $obj) and inside it echo $obj to echo $key;. Here is simplified working example. ALTERNATIVE SOLUTION If working foreach is not your goal but rather pretty printed json, then instead use following code: $json_string = json_encode($data, JSON_PRETTY_PRINT); echo $json_string;
How can I parse this JSON with PHP?
How can I parse this JSON, which is supposed to display the items a user has in their Steam inventory. I have tried this: $data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/'); $json = json_decode($data); echo $data; It returns the same as just visiting the link. I can't get anything like this to work either: $id = $json->type; echo $type;
This is how to get type $data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/'); $json = json_decode($data); foreach ($json->rgDescriptions as $mydata) { echo $mydata->type; }
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/'); $json = json_decode($data); echo $data; you are echoing $data that is your input (so you see the same as opening the link directly). To see if the json_decode is working fine you should print $json. So instead of echo $data; use echo '<pre>' print_r($json); echo '</pre>';
$data = file_get_contents('http://steamcommunity.com/id/Mitch8910/inventory/json/440/2/'); $json = json_decode($data); Now $json has 2 objects. you can access like. $json->rgInventory; $json->success; if you want to fetch all data from $json->rgInventory; foreach($json->rgInventory as $e){ //var_dump($e); echo $e->id; echo $e->classid; echo $e->instanceid; } etc.
Parsing JSON array a few levels deep in php
I am pretty new to json and arrays and I have the following but I am not getting any results from a Jenkins JSON output. Link to output - http://resonant-rise.com/mcupdater/parser.php Code $url = "http://jenkins.jakimfett.com/job/AlchemyPlusPlus/lastSuccessfulBuild/api/json?pretty=true"; $output = json_decode(file_get_contents($url), true); print "<pre>"; print_r($output); print "</pre>"; foreach ($output->actions as $item) { echo $item->fullDisplayName . "\n"; }
<?php $url="http://jenkins.jakimfett.com/job/AlchemyPlusPlus/lastSuccessfulBuild/api/json?pretty=true"; $output = json_decode(file_get_contents($url), true); echo "<pre>"; print_r($output); echo "</pre>"; /* for properties like fullDisplayName, id, keeplog etc .. directly use echo */ echo $output['fullDisplayName']; /* for properties like actions and artifacts */ foreach($output['actions'] as $action){ print_r($action); /* actions itself is array so use print_r */ } ?>
Getting data from JSON with PHP [duplicate]
This question already has an answer here: How to extract and access data from JSON with PHP? (1 answer) Closed 11 months ago. Here's the json [{"location":"USA","email":"test#test.com","sex":"male","age":"Unkown","other":null,"profile":{"net":["55","56"],"networks":[{"site_url":"http://site.com","network":"test","username":"mike"},{"site_url":"http://site.com/2","network":"test2","username":"mike2"}]},"name":"Mike Jones","id":111}] I wanted to know how I could echo out all networks so it echos out the site_url,network, and user for each of the 2. How would I get "name" at the end out of there as well? Tanks!
Use json_decode() http://php.net/manual/en/function.json-decode.php $data = json_decode(...your sstring ...); echo $data[0]->name;
Use json_decode to decode the JSON data. Then you can iterate the array with foreach and access the site_urls of each array item with another foreach like: $arr = json_decode($json); foreach ($arr as $obj) { foreach ($obj->profile->networks as $network) { echo $network->site_url; } }
http://lt2.php.net/json_decode
Here's a straight-forward example doing the two things that you ask for (see inline comments). $json = '[{"location":"USA","email":"test#test.com","sex":"male","age":"Unkown","other":null,"profile":{"net":["55","56"],"networks":[{"site_url":"http://site.com","network":"test","username":"mike"},{"site_url":"http://site.com/2","network":"test2","username":"mike2"}]},"name":"Mike Jones","id":111}]'; // "Decode" JSON into (dumb) native PHP object $data = json_decode($json); // Get the first item in the array (there is only one) $item = $data[0]; // Loop over the profile.networks array foreach ($item->profile->networks as $network) { // echos out the site_url,network, and user echo "site_url = " . $network->site_url . PHP_EOL; echo "network = " . $network->network . PHP_EOL; echo "user = " . $network->username . PHP_EOL; } // Get "name" at the end echo "name = " . $item->name . PHP_EOL; It should output (if you're viewing as HTML, it will be munged onto one line… don't output as HTML). site_url = http://site.com network = test user = mike site_url = http://site.com/2 network = test2 user = mike2 name = Mike Jones
Building on aviv's answer... $data = json_decode(...your sstring ...); echo $data[0]->location; // USA ... echo $data[0]->profile->net[0]; // 55 echo $data[0]->profile->net[1]; // 56 echo $data[0]->profile->networks[0]->site_url; // http://site.com echo $data[0]->profile->networks[0]->network; // test echo $data[0]->profile->networks[0]->username; // mike echo $data[0]->profile->networks[1]->site_url; // http://site.com/2 echo $data[0]->profile->networks[1]->network; // test2 echo $data[0]->profile->networks[1]->username; // mike2 echo $data[0]->name; // Mike Jones