How to add Array Object and Data to JSON in Laravel - php

How to add Array Object and Data to JSON in Laravel
$json = {'id':5, 'name':'Hassan'};
I want to add new object role and value Admin to $json
I want result like
$json = {'id':5, 'name':'Hassan', 'role':'Admin'};

You could decode the JSON, add the values you want and then encode it back to JSON:
$data = json_decode($json, true);
$data['role'] = 'Admin';
$json = json_encode($json);
json_decode() Docs
json_encode() Docs

Try this
$object = json_decode($json);
$object->role = 'Admin';
$json = json_encode($object)

Related

Accessing the results from CURL request

Im making a CURL request in PHP, and trying to echo the ID result.
$responseData = request();
echo $responseData["id"];
The results are
{
"result":{
"code":"000.200.100",
"description":"successfully created checkout"
},
"buildNumber":"3b5605d6df9e6068ff1e9b178947fc41e641456e#2019-11-26 03:42:27 +0000",
"timestamp":"2019-11-27 13:19:29+0000",
"ndc":"B244D47DBFAD13EDEF126C980A711C8D.uat01-vm-tx02",
"id":"B244D47DBFAD13EDEF126C980A711C8D.uat01-vm-tx02"
}
But when trying to get the ID i am getting the error
Warning: Illegal string offset 'id' in \public\payment\index.php on line 25
JSON can easily be parsed in PHP using json_decode(). There is an optional flag to decode the JSON into an object or an array.
Decode as an object:
$json = request();
$data = json_decode($json);
echo $data->id;
Decode as an array:
$json = request();
$data = json_decode($json, TRUE);
echo $data['id'];
if you can decode you json using json_decode and then access the id variable like you are doing will be good for you.
$data = request();
$responseData = json_decode($data);
echo $responseData["id"];

Fetching data attributes from instagram hashtag

I want to fetch data(the images in each post) stored in https://www.instagram.com/explore/tags/selfie/?__a=1, but all I get when I decode and var_dump this is NULL.
$obj = json_decode("https://www.instagram.com/explore/tags/selfie/?__a=1", true);
var_dump($obj);
Before decoding json you have to first fetch api response.
$obj = json_decode(file_get_contents("https://www.instagram.com/explore/tags/selfie/?__a=1"), true);
You're trying to json_decode the STRING
https://www.instagram.com/explore/tags/selfie/?__a=1
What you need to do is to fetch the URL first. I suggest using file_get_contents, which takes a URL and returns the contents at the end of that URL.
Try this:
$json = file_get_contents("https://www.instagram.com/explore/tags/selfie/?__a=1");
$obj = json_decode($json, true);
var_dump($obj);
The argument to the function json_decode(), $html must be plaintext/string.
This should work.
$url = "https://www.instagram.com/explore/tags/selfie/?__a=1";
$html = file_get_contents($url);
$obj = json_decode($html,true);
var_dump($obj);
See this in action here

I am doing CRUD operation by using api.php from github and want to fetch id or name and save into a varibale

$response = call('GET','http://localhost/ProjectCamera/projectcamera/api.php/state');
$jsonObject = json_decode($response, true);
$jsonObject = php_crud_api_transform($jsonObject);
$output = json_encode($jsonObject, JSON_PRETTY_PRINT);
$state_id = $output->state_id;
print_r($state_id);
?>
Since $jsonObject is already an object you don't need to json_encode again with JSON_PRETTY_PRINT instead you should use json_decode($jsonObject) this will convert it to stdClass Object then you can access the state_id property/field.
you can try the below code.
$response = call('GET','http://localhost/ProjectCamera/projectcamera/api.php/state');
$jsonObject = json_decode($response, true);
$jsonObject = php_crud_api_transform($jsonObject);
//If $jsonObject is an JSON object otherwise
//If php_crud_api_transform is returning the array then you need to used json_encode but without JSON_PRETTY_PRINT
//BY passing true in json_decode second parameter will convert it to array
$output = json_decode(json_encode($jsonObject),true);
//you will get first state id
$state_id = $output['state'][0]['state_id'];
print_r($state_id);

Parse Data from JSON URL

I'm trying to get data onto my website by parsing data from a JSON format. The URL is http://api.bfhstats.com/api/onlinePlayers and I'm trying to output for example the currently players online on PC.
Here's the current code I have:
<?$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data = json_decode($jsondata, true);
echo $data->pc->peak24;?>
I thought this would work, however it's not displaying anything. I am very new to parsing JSON data so if someone could explain what I'm doing wrong that would be brilliant.
change:
$data = json_decode($jsondata, true);
to
$data = json_decode($json, true);
Also, json_decode returns an array so use:
echo $data['pc']['peak24'];
to access the data.
You first call the variable $json but then use $jsondata in json_decode.
You are missing the foreach cycle to fetch the two dimensional array $data:
<?php
$json = file_get_contents("http://api.bfhstats.com/api/onlinePlayers");
$data=array();
$data = json_decode($json, true);
//print_r ($data);
foreach ($data as $pc) {
echo $pc["peak24"]."<br>";
}
?>
Check the $json and $jsondata that have different name but should be the same.

get Specific JSON Response Value

json response as below:
{"Success":true,"ErrorCode":0,"UserInformation":{"UserID":"19"}......
how do I get the UserID value by using json_decode?
I tried the below code with no luck.
$Response = json_decode($Response[2]);
echo $Response[0][0][0];
This should work:
<?php
$json = '{"Success":true, "ErrorCode":"0","UserInformation":{"UserID":"19"}}';
$response = json_decode($json, true);
echo $userId = $response["UserInformation"]["UserID"];
?>
Please check the PHP documentation of json_decode at http://php.net/json_decode
You can use object as well if you skip the second parameter of the json_decode function.

Categories