I have a postman request with some data in the body like that :
{
"informations" : {
"contact" :[
{
"email" : 'email#email.fr',
"phone" : 0111111111,
}
]
}
}
I symfony side, I can get theses data with :
$data = $request->getContent();
dd($data);
But I can't access to the "contact" array, I tried this :
dd($data['informations']['contact']);
And I get this error : Warning: Illegal string offset 'informations'
What is my mistake ? thanks
EDIT :
I also tried to use decode_json :
$data = json_decode($data, true);
dd($data);
And it return me NULL
I tried a json_last_error_message() and it return me a "Syntax error"
It is related with phone type. 'Numbers in JSON must be an integer or a floating point.' Integer(phone number) can not start with 0. You can convert to string or replace the zero.
You can try with
{
"informations" : {
"contact" :[
{
"email" : "email#email.fr",
"phone" : "0111111111"
}
]
}
}
OR
{
"informations" : {
"contact" :[
{
"email" : "email#email.fr",
"phone" : 111111111
}
]
}
}
Related
I want to read json file from database directory, but give The Response content must be a string or object implementing __toString(), "boolean" given. error. Bellow is my code :
$json = file_get_contents('database/query_templates/course.json');
$data = json_decode($json, TRUE);
echo '<pre/>'; print_r($data); exit;
course.json file content :
{
"post_filter" : {
"operator" : [
{
"operator" : [
{
"range" : {
"created_at" : {
"gte" : "log_start_date",
"lte" : "log_end_date",
"format" : "yyyy-MM-dd"
}
}
},
{
"terms" : {
"search_param_user_action.id" : "user_select_array"
}
}
]
},
{
"operator" : [
{
"operator" : [
"conduct_days_multi"
]
},
{
"operator" : [
"dropdowns"
]
}
]
},
{
"query" : {
"multi_match" : {
"query" : "x",
"fields" : [
"resource_search_columns"
],
"operator" : "operator"
}
}
}
]
}
}
for any help thanks.
Sometimes some php releasees not supported get_file_contents() or this func return type string but not only return string everytime, sometimes return boolean value bacause it's working on the byte codes, you can try traditional file operation methods or you can try convert to string.
I am having some issues parsing a json file from Jenkins using PHP
{
"actions" : [
{
"causes" : [
{
"shortDescription" : "Started by an SCM change"
}
]
},
{
},
{
},
{
"buildsByBranchName" : {
"origin/release_5.6.0" : {
"buildNumber" : 242,
"buildResult" : null,
"marked" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
},
"revision" : {
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"branch" : [
{
"SHA1" : "fde4cfd86b8511d328037b9e9c55876007bb6e67",
"name" : "origin/release_5.6.0"
}
]
}
},
"origin/release_5.7.0" : {
"buildNumber" : 315,
"buildResult" : null,
"marked" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
},
"revision" : {
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"branch" : [
{
"SHA1" : "ae2cbf69a25e0632e0f1d3eeb27a907b154efce0",
"name" : "origin/release_5.7.0"
}
]
}
},
I have tried doing the following
//Read in JSON object
$json_file2 = file_get_contents('url.com/json');
//Decode JSON file
$test = json_decode($json_file2); //object
//print_r($json_file2);
echo $test->causes;
I am also trying to access the different sections in "buildsByBranchName". I have tried many different variations of the code above, but I keep getting "Undefined property: stdClass" errors.
You are not accessing that value properly. causes resides under actions which is an array. Your code also won't work because causes is an array.
// This is an array so you can't use echo here.
$causes = $test->actions[0]->causes;
// echo out the shortDescription
echo $causes[0]->shortDescription;
or
echo $test->actions[0]->causes[0]->shortDescription;
I am trying to modify my JSON file with a PHP solution :
From this file :
[
{
"id":40764465700014,
"Pays":"France",
"lat":43.221401,
"long":6.1424990000000435
}
]
To this file :
[
{"id" : "40764465700014",
"Pays" : { "add" : "France" },
"lat" : { "add" : 43.221401 },
"long" : { "add" : 6.1424990000000435}
}
]
Do you have an idea ?
Thanks
Just use json_decode() and convert it to array , then just encode the array into json using json_encode(). Use the code below
<?php
$json = '[
{
"id":40764465700014,
"Pays":"France",
"lat":43.221401,
"long":6.1424990000000435
}
]';
$array= json_decode($json,true);
echo json_encode($array[0]);
Hope this helps you
I got this kind of document:
{
"_id" : ObjectId("54ad5c3b9a703a3c088b4567"),
"hard" : 750,
"coordinates" : {
"x" : 0.2388169910939489,
"y" : 0.7996551291084174
},
"indicator" : 500,
"networkIdList" : {
"networkIdData" : [
{
"networkId" : "abc123",
"type" : "SomeNetwork"
},
{
"networkId" : "123asdf",
"type" : "AnotherNetWork"
},
{
"networkId" : "abc123",
"type" : "OneMoreNetwork"
}
]
}
}
And I need to perform a query to find the document that have "networkId" = "abc123" AND "type" = "SomeNetwork".
I have tried With this instruction:
$this->documentManager->createQueryBuilder('Mydocument') ->field('networkIdList.networkIdData.$.networkGamingId')->equals('abc123') ->field('networkIdList.networkIdData.$.type')->equals('')
->getQuery()
->execute());
But the cursor return no data.
I also try with
->where("function() {return this.networkIdList.networkIdData.$.networkGamingId == 'abc123'}")
but in this case i got an error that says the Object $ has no propierties.
And I need to perform a query to find the document that have "networkId" = "abc123" AND "type" = "SomeNetwork"
$qb = $dm->createQueryBuilder('Foo')
->field('networkIdList.networkIdData.networkId')->equals('abc123')
->field('networkIdList.networkIdData.type')->equals('SomeNetwork');
I understand how to parse json with PHP, however I don't understand how to read it with the eye. Can someone please help me understanad this?
Here is my code
<?php
$json = file_get_contents('json.txt');
$json_output = json_decode($json);
foreach ( $json_output->query as $stf )
{
echo "{$stf->response->domains->name}\n";
}
?>
Here is a sample of the json result
{ "query" : { "host" : "test.com",
"tool" : "pro"
},
"response" : { "domain_count" : "13",
"domains" : [ { "last_resolved" : "2012-01-11",
"name" : "test1.com"
},
{ "last_resolved" : "2012-01-11",
"name" : "test2.com"
},
As you can see I tried query->response->domains->name and it didn't work.
How would I tried name?
Thank you in advance
query->response->domains is an indexed array, so you need to get an index, say [0], and then get the ->name from that.
echo $stf->response->domains[0]->name."\n";
foreach ( $json_output->query->response->domains as $domain )
{
echo $domain->name;
}
Study this http://json.org/
If you're trying to read it by eye, it might help to reformat:
{
"query" : {
"host" : "test.com",
"tool" : "pro"
},
"response" : {
"domain_count" : "13",
"domains" : [{
"last_resolved" : "2012-01-11",
"name" : "test1.com"
},{
"last_resolved" : "2012-01-11",
"name" : "test2.com"
}]
}
}