How to get values from json object to php array - php

I am writing a php file for an application that allows the user to print a project report (with fpdf library).
The aim is to get datas from a db and then to build the PDF file dynamically.
The datas are stored in json.
It is ok for almost all datas, but there are some that I can't reach.
Here is an example :
First of all I already did this :
$Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false);
To get the number "nb" of "A523500" I'm doing like that :
$Array['roof_coordinates']->results->packingList->total->A523500->nb;
What am I doing wrong ?
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
},
},
}
},
}
And I tried to pass "true" to json_decode to convert objects to associative array but it doesn't seems to work...
Any help will be great !
Thank you in advance ;)

Make sure you are accessing the resulting data structure correctly, it should work whether you decode to an array or an object.
<?php
$json = <<<END
{
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
}
}
}
}
}
}
END;
$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];
$stdClassResults = json_decode($json);
$nbFromStdClass = $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;
assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');
echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;

Related

PHP JSON: Get value from siblings reference

I am working on a project that requires reading data from this terrible API that responds with terrible structured JSON data:
"finn-contanct": {...}
"finn-adata": {
"#attributes": {
"model": "https://cache.api.finn.no/iad/ad/model/car-used-sale"
},
"finn-field": [
{
"#attributes": {
"name": "authorized_dealership",
"value": "true"
}
},
{
"#attributes": {
"name": "body_type",
"value": "Stasjonsvogn"
}
},
{
"#attributes": {
"name": "car_location",
"value": "Norge"
}
},
{
"#attributes": {
"name": "engine"
},
"finn-field": [
{
"#attributes": {
"name": "effect",
"value": "90"
}
},
{
"#attributes": {
"name": "fuel",
"value": "Diesel"
}
}
]
},
{...},
]
}
How can i dynamically get the values under each attribute based on the siblings name value? Ideally with a function that accepts one parameter that finds the value in there by providing a key corresponding to the value i'm looking for.
Here is an example of what i'm expecting:
Given a function that expects one parameter: getAttrValue('key') I want to get the value under the #attributes sibling. So if I use the function like this: getAttrValue('body_type') i'm simply expecting this back: Stasjonsvogn. I don't really care about nested items. So if I do this: getAttrValue('fuel') I'm simply expecting: Diesel
I found this answer here on SO. But the problem with that method is that it doesn't work well with nested items. So does anyone have a method that would work with the data-structure I got above here?
The response is a total mess and I don't know how to handle it, nor how to Google it properly. So any help would be appreciated greatly.
Collect attributes into associative array recursively:
function collectAttributes($data)
{
$attributes = [];
$nodeAttribute = isset($data['#attributes']) ? $data['#attributes'] : [];
//collect current node attribute value
if (isset($nodeAttribute['name'])) {
$attributes[$nodeAttribute['name']] = isset($nodeAttribute['value']) ? $nodeAttribute['value'] : '';
}
//collect nested attributes recursively
foreach ($data as $nestedNode) {
if (is_array($nestedNode)) {
$attributes = array_merge($attributes, collectAttributes($nestedNode));
}
}
return $attributes;
}
And then use result as simple associative array:
$data = json_decode($inputJson, true);
$atttributes = collectAttributes($data);
echo $attributes['fuel']; //don't forget isset checking if you are not sure about content
But if you have attributes with same name you'll see only latest this way.

how to retrieve json data in sequence

i have simple json results like below
{
"code": 200,
"image": "https://example.com/image.jpg",
"result": [
{
"url": "https://example.com/1",
"label": "MP4"
},
{
"url": "https://example.com/2",
"label": "FLV"
},
{
"url": "https://example.com/3",
"label": "MP3"
},
{
"url": "https://example.com/4",
"label": "AVI"
},
{
"url": "https://example.com/5",
"label": "WMV"
}
]
}
as you can see, that there are so many different label paths, and sometimes the sequence of labels is not like the above, it's always changing (random), and I'm trying to get the MP3 label part but can not.
My question is, how to take the sequence of json which has MP3 label?
I have tried with the script below
$uri = json_decode(file_get_contents('https://example.com/json.json'),TRUE);
echo $uri['result'][2]['url'];
but as described above, the position of the MP3 label is always changing, is there a way to overcome it?
Try following code:
$nodesWithMp3Labels = array_map(function($a) {
if (strtolower($a["label"]) == "mp3") {
return $a;
}
}, $uri['result']);
Read more about array_map
With array_map, there will be some blank values if label is not mp3.
Another solution using simple foreach loop:
$nodesWithMp3Labels = [];
foreach ($uri['result'] as $a) {
if (strtolower($a["label"]) == "mp3") {
$nodesWithMp3Labels[] = $a;
}
}
You should use array_filter to get all the results with MP3 label:
$uri = json_decode(file_get_contents('https://example.com/json.json'), true);
$mp3 = array_filter($uri['result'], function($item) {
return $item['label'] === 'MP3';
})

php foreach Iteration through json

Assume that I have below json response fetched from an API.
{
"owner": "Jane Doe",
"pets": [
{
"color": "white",
"type": "cat"
},
{
"color": "black",
"type": "dog"
}
]
}
From the below PHP code I have converted the json string into a json object. And displayed the pet types.
$jsonObject = json_decode($json);
foreach($jsonObject->pets as $pets){
echo 'Pet type:'.$pets->type.'</br>';
}
However in some cases the response json from the API is in below format
{
"owner": "John Doe",
"pets": {
"color": "white",
"type": "cat"
}
}
in this case above php foreach iteration fails with below message
*Notice: Trying to get property of non-object *
I'm looking for a easy way to do this because the actual json response which i'm handling has lot of these occurrences.
You need to check whether $jsonObject->pets is an array or object. If it's an object, replace it with an array containing that object, then the loop will work.
if (!is_array($jsonObject->pets)) {
$jsonObject->pets = array($jsonObject->pets);
}
You could also do it with a conditional in the foreach:
foreach (is_array($jsonObject->pets) ? $jsonObject->pets : [jsonObject->pets] as $pet) {
...
}
If you don't want to have to write all that every time, you could put it in a function:
function as_array($x) {
return is_array($x) ? $x : [x];
}
and then use:
foreach (as_array($jsonObject->pets) as $pet) {
...
}
I'd also complain to the API designer. It's ridiculous to return inconsistent data formats like this.

Decoding and looping through PHP JSON array

I have the following deocoded JSON array.
I need to access the "type" inside the context, as well as I need to loop through each of the values in the payload. How do I do that?
{
"RequestHeader":
{
"mess": "am putting it on my wall....",
"created_time": "2010-08-24T09:01:25+0000"
},
"context" :
{
"type": "friends,circles"
}
"payload" [ {12345},{12345} ,{2345} ]
}
I tried the following, but it doesn't work
$decoded = json_decode($json_string);
for ($i=0;$i<payload.length;++$i)
{
$id=$decoded->payload[$i];
//do some operation with the id
}
First of all the JSON you provided is invalid. Supposedly the valid one should look like this
{
"RequestHeader": {
"mess": "am putting it on my wall....",
"created_time": "2010-08-24T09:01:25+0000"
},
"context": {
"type": "friends,circles"
},
"payload": [
12345,
12345,
2345
]
}
After you've fixed the problem with JSON provider it will be quite easy to access data
<?php
$json = <<<'JSON'
{
"RequestHeader": {
"mess": "am putting it on my wall....",
"created_time": "2010-08-24T09:01:25+0000"
},
"context": {
"type": "friends,circles"
},
"payload": [
12345,
12345,
2345
]
}
JSON;
$data = json_decode($json, true);
$type = $data['context']['type'];
var_dump($type);
foreach($data['payload'] as $id) {
var_dump($id);
}
Remember to make sure you check that the data actually exists before accessing it, e.g. isset($data['context']['type']) unless you are absolutely sure in it's integrity.
When you use json_decode method, the output will be nested arrays
So for example to access context type you need to do the following
echo $decoded["context"]["type"];
And to loop on payload you need to do the following
for ($i=0;$i<$decoded["payload"].length;++$i)
{
$id=$decoded["payload"][$i];
//do some operation with the id
}

PHP: get data from json array without loop

I have data i want to get from array without loop.
I want to get "value" of "link_click" for example, how can i make this work?
I tried: but this not working.
$stats = json_decode($data, false);
$link_click= $stats->data->actions->action_type->['link_click']->value;
{
"data": [
{
"actions": [
{
"action_type": "comment",
"value": 2
},
{
"action_type": "link_click",
"value": 636
},
{
"action_type": "post_like",
"value": 2
},
{
"action_type": "page_engagement",
"value": 640
},
{
"action_type": "post_engagement",
"value": 640
}
],
The only way you can make it possible only if you know the index of the action_type:link_click. If you know the index, you can do it by. (Answer is with respect to the data you have shown above).
$stats = json_decode($data, true);
$link_click= $stats['data']['actions'][1]['value'];
Loop example (on request):
$stats = json_decode($data, true);
$value = 0;
foreach($stats['data']['actions'] as $action) {
if ($action->action_type == 'link_click') {
$value = $action->value;
}
}
echo $value; //This is your value
You can use something like JsonPath to get the value

Categories