Laravel 5 read Json file give error `Response.php line 403` - php

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.

Related

get data from postman request with data in body

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
}
]
}
}

Delete a specific data from mongo

I want to delete specific data from mongo. below is my json. I want to delete all occurence of IIT-395.
{
"_id" : "i34908s",
"lifeStageCourses" : [
{
"lifeStage" : "in_school",
"tags" : [
"IIT-182"
],
"courseIds" : []
},
{
"lifeStage" : "in_college",
"tags" : [
"IIT-134",
"IIT-140",
"IIT-395"
],
"courseIds" : []
},
{
"lifeStage" : "prep_entrance_exam",
"tags" : [],
"courseIds" : []
},
{
"lifeStage" : "job_seeker",
"tags" : [
"IIT-134",
"IIT-395"
],
"courseIds" : []
}
]
}
I expect output only IIT-395 should be removed
Instead whole document is getting deleted.
try following query .
db.getCollection('careerpaths').update(
{"lifeStageCourses.tags" : "IIT-395"},
{$pull : {
"lifeStageCourses.$[].tags" :"IIT-395"
}},false, true
);

Parse json with PHP. Getting Undefined property: stdClass errors

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;

Find a document with Doctrine ODM with equals condition on nested array of objects

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');

Parsing JSON results

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"
}]
}
}

Categories