{
"phone_number": "918358808742",
"confirmation_token": "c86798c29bd811e58f5822000b9964d9",
"experiment_data": {
"opt_in_consent": 7,
"conf_code_auto_submit_ff": 1,
"manual_referral_code_switch": 1,
"conf_code_auto_submit": 1,
"registration_reminders": 1,
"reengagement_gcm_switch": 1,
"kraken_app_progress_meter_v2": 1,
"kraken_background_service_control": 1,
"modified_sms_confirmation_page": 1,
"auto_sms_confirmation": 1,
"registration_express": 1,
"tutorial_before_offer_start": 1,
"fb_connect_active": 1,
"fb_connect_required_for_login": 1,
"auto_sms_confirmation_v2": 1,
"pwd_input_variations": 4,
"registration_reminder_notifications": 1,
"track_adwords_install": 1
},
"member_id": "a2mx2z"
}
I want this data to be in array form any help will be appreciated
You could use json_decode() function, here a simple exmple
$json = {};
$array = json_decode($json, true); // if you would like arrays
$object = json_decode($json); // if you would like object
echo $array['phone_number']; // array example
echo $object->phone_number; // object example
if you would like to test the validation of your json before decode it you can do it using json_encode() function
$json = {};
if(json_encode($json) == false) {
die("incorrect json !");
} else {
// do the decoding
}
Related
Trying to get the a certain parameter in my multidimensional array... I have the following api request:
$responeAPI= ClientCode::request('post', 'responeAPI', $responeAPI,['debug'=>true]);
foreach ($responeAPI["buDetail"] as $key => $value){
$businessUserDet = $value["name"];
}
$storage['enterpriseList'] = $businessUserDet;
}
The array from the API response is like this:
{
"buList": {
"buDetail": [
{
"businessUserId": 2,
"name": "SAMPLENAME_231",
"parentBusinessUserId": 1,
"profileId": 2,
"profileName": "Enterprise"
}
]
},
"resultCode": 0,
"transactionId": "responeAPIs_1577358460"
}
I need to extract the "name" so I can use it for the $options parameter. Right now, my code isn't showing anything.
You can do like this using array_column
$jsonArray = '{"buList":{"buDetail":[{"businessUserId":2,"name":"SAMPLENAME_231","parentBusinessUserId":1,"profileId":2,"profileName":"Enterprise"},{"businessUserId":2,"name":"SAMPLENAME_231","parentBusinessUserId":1,"profileId":2,"profileName":"Enterprise"}]},"resultCode":0,"transactionId":"responeAPIs_1577358460"}';
$detais = json_decode($jsonArray, true);
print_r(array_column($detais['buList']['buDetail'], 'name', 'id'));
http://sandbox.onlinephpfunctions.com/code/458592622c78c67771cbf2e54661b9294c91e710
Could you change this line
foreach ($responeAPI["buDetail"] as $key => $value){
to
foreach ($responeAPI["buList"]["buDetail"] as $key => $value){
You can invoke as an object:
$array = '{ "buList": { "buDetail": [{ "businessUserId": 2, "name": "SAMPLENAME_231", "parentBusinessUserId": 1, "profileId": 2, "profileName": "Enterprise" } ] }, "resultCode": 0, "transactionId": "responeAPIs_1577358460" }';
$array = json_decode($array);
//for debug purposes only
echo var_export($array, true);
echo $array->buList->buDetail[0]->name;
Output: SAMPLENAME_231
I have 2 different jsons and I need to get one inside the other.
JSON 1
[{
"id":"1",
"texto":"Vamos newells 17471934",
"fecha":"2019-06-24 12:09:12",
"categoria":"1",
"idpayment":"Reference_1561388952",
"provincia":"1",
"estado":"NO",
"email":"newells#gmail.com"
}]
JSON 2
{
"Texto": " VENDO O PERMUTO",
"imageJob": {
"pathConvertido": "ClasificadosPNG/0011311247.png",
"convertido": true,
"id": 5011
},
"rubroClasificado": {
"CodigoRubro": 150,
"id": 76
}
}
I need the second one inside the first one for use with javascript
I tried the array_merge() php function without getting results
since JSON1 is an array of JSON-object and JSON2 is just a JSON-object.
in addition to that you said, 'I need the second one inside the first one for use with javascript'. therefore, you can test this code https://3v4l.org/1HGNF/perf#output
please compare all performances in mem-usage and timing
$j1 = '[{
"id":"1",
"texto":"Vamos newells 17471934",
"fecha":"2019-06-24 12:09:12",
"categoria":"1",
"idpayment":"Reference_1561388952",
"provincia":"1",
"estado":"NO",
"email":"newells#gmail.com"
}]';
$j2 = '{
"Texto": " VENDO O PERMUTO",
"imageJob": {
"pathConvertido": "ClasificadosPNG/0011311247.png",
"convertido": true,
"id": 5011
},
"rubroClasificado": {
"CodigoRubro": 150,
"id": 76
}
}';
$j1 = json_decode($j1, true);
$j2 = json_decode($j2, true);
$j1[] = $j2;
var_dump(json_encode($j1));
You can use json_decode with parameter true to convert JSON into an array , then use array_merge to make them a single array
$j1 = '[{"id":"1","texto":"Vamos newells 17471934","fecha":"2019-06-24 12:09:12","categoria":"1","idpayment":"Reference_1561388952","provincia":"1","estado":"NO","email":"newells#gmail.com"}]';
$j1arr = json_decode($j1, true);
$j2 = ' {
"Texto": " VENDO O PERMUTO",
"imageJob": {
"pathConvertido": "ClasificadosPNG/0011311247.png",
"convertido": true,
"id": 5011
},
"rubroClasificado": {
"CodigoRubro": 150,
"id": 76
}
}';
$j2arr = json_decode($j2, true);
$r = array_merge($j1arr[0], $j2arr);
You can use the merged array in javascript by using json_encode
json_encode($r)
https://3v4l.org/WKX1U
Do it simply like this way without any extra array merging methods-
<?php
$json1 = json_decode('[{"id":"1","texto":"Vamos newells 17471934","fecha":"2019-06-24 12:09:12","categoria":"1","idpayment":"Reference_1561388952","provincia":"1","estado":"NO","email":"newells#gmail.com"}]',1);
$json2 = json_decode('{"Texto":" VENDO O PERMUTO","imageJob":{"pathConvertido":"ClasificadosPNG/0011311247.png","convertido":true,"id":5011},"rubroClasificado":{"CodigoRubro":150,"id":76}}',1);
$json1[] = $json2; // add second json to first json
print_r($json1);
echo json_encode($json1);
?>
WORKING DEMO: https://3v4l.org/qhsJq
As you said you want to process the resulting data in JavaScript, and as per your question you want second json inside the first json. You can do something like this.
You will get a json as final result.
$first = '[{
"id":"1",
"texto":"Vamos newells 17471934",
"fecha":"2019-06-24 12:09:12",
"categoria":"1",
"idpayment":"Reference_1561388952",
"provincia":"1",
"estado":"NO",
"email":"newells#gmail.com"
}]';
$first = str_replace(['[',']','}'], '', $first);
$second = '{
"Texto": " VENDO O PERMUTO",
"imageJob": {
"pathConvertido": "ClasificadosPNG/0011311247.png",
"convertido": true,
"id": 5011
},
"rubroClasificado": {
"CodigoRubro": 150,
"id": 76
}
}';
$second = preg_replace('/\{/', ',', $second,1);
print_r($first.$second);
As result you will get a valid json, second json inside your first json, you can validate here
I need PHP JSON help.
I have current output :
{
"status": 200,
"response_msec": 15,
"data": {
"android": {
"test1": 15,
"test2": 6,
"test3": 15,
"test4": 101,
"test5": 87,
"test6": 8,
"test9": 119,
"test10": 101,
"test11": 107
}
}
}
I need print this value : test1 , test2 , test3 ...,test11 .
I have tested some method :
$json = json_decode($result, true);
$dec = (Array)json_decode($result);
print_r ($dec["android"]);
and
foreach ($array as $value)
{
echo $value->android;
}
But not work.
You are missing the ['data'] key,
<?php
$json = '{"status":200,"response_msec":15,"data":{"android":{"test1":15,"test2":6,"test3":15,"test4":101,"test5":87,"test6":8,"test9":119,"test10":101,"test11":107}}}';
$array = json_decode($json, true);
var_dump(array_keys($array['data']['android']));
Check here i have made a php sandbox http://sandbox.onlinephpfunctions.com/code/6f97a9bb499b54919b40d4d12f49049fdd732aef
Also, You can use the function array_keys() to get only the keys of an array, that's what i did.
Your code should work if the json string is assigned to $value. It's just you forgot to include to get the data "data" from "value". Your 3rd line of the first method should look like this:
print_r ($dec["data"]["android"]);
Have a great day
I have this function in php
<?php function showTable(){
$url = "http://10.0.0.1/lib/api/desk/branch/";
$params = array ("action" => "list","company_key" => "1");
$result=requestURL($url,$params);
$json_a=json_decode(strip_tags($result),true);
?>
This is the json I got from the code above. I need to check the key "status". If it has a value "no", it should display an alert that status is not ok. Thanks!
{
"init": [
{
"status": "ok",
"record_count": 9,
"code": "",
"message": "",
"page_count": null,
"current_page": 0
}
]
}
Easy.
Suppose you hold the JSON in $json.
$jsonDecoded = json_decode($json, true);
if($jsonDecoded['init'][0]['status'] != 'ok') {
print "status is not ok :(";
}
Let say that json is in $json variable
$json = json_decode($json,true);
$status = $json['init'][0]['status'];
if($status=='ok'){
# do something here
}else{
# display not ok here
}
My application has a list of reports and each 'Report' is defined as a new object in PHP. I'm trying to make a basic API and I want to show a list of all the 'Reports' in 1 JSON string. My code below works well for encoding 1 object into JSON, but how do I get ALL objects and turn ALL of them into 1 long JSON string?
I understand I could make an array of objects, but that returns these brackets [] and I don't want those brackets in the JSON string.
class report {
public $report_name;
public $report_value;
public $report_benchmark;
public $report_result;
public function __construct($report_name, $report_value, $report_benchmark, $report_result) {
$this->report_name = $report_name;
$this->report_value = $report_value;
$this->report_benchmark = $report_benchmark;
$this->report_result = $report_result;
}
}
$item = new report('test', 0, 0, 'OK');
echo json_encode($item, JSON_PRETTY_PRINT);
As suggested by #JimL, you can simply add some objects in an array, which you can encode as json latter on.
$item = array();
$item[] = new report('test', 0, 0, 'OK');
$item[] = new report('test2', 0, 0, 'OK');
echo json_encode($item, JSON_PRETTY_PRINT);
produces:
[{
"report_name": "test",
"report_value": 0,
"report_benchmark": 0,
"report_result": "OK"
}, {
"report_name": "test",
"report_value": 0,
"report_benchmark": 0,
"report_result": "OK"
}]
Note here that you can validate the produced json here
You can do some thing like this.
$item = array();
$item['report1'] = $item = new report('test', 0, 0, 'OK');
$item['report2'] = $item = new report2('test', 0, 0, 'OK');
$item['report3'] = $item = new report3('test', 0, 0, 'OK');
echo json_encode($item, JSON_PRETTY_PRINT);
make an associative array and add assign all objects a meaning full key, which will help to easy access each specific report.