PHP - Append new JSON object to nested array within JSON object - php

Setup
I have retrieved a JSON string from a URL using 'file_get_contents' and have subsequently decoded it using `json_decode'
$search_URL="http://abcdefg.com"
$json = file_get_contents($search_URL);
$obj = json_decode($json, true);
The JSON represents an Apache Solr search response:
{
"responseHeader":{
"status":0,
"QTime":3,
"params":{
"q":"zzzzzzz",
"wt":"json"
}
},
"response":{
"numFound":20690,
"start":0,
"docs":[
{
"title":"yyyyy-y-y-yyyyy-yy-y",
"author":"author 1, author 2, author 3",
"url":"https://yyyyy.com",
"id":yyyy,
"_version_":yyyyy
},
{
"title":"xxxx-x-x-xxxx-xxxx",
"author":"author 1, author 2",
"url":"https://xxxxx.com",
"id":xxxxx,
"_version_":xxxxxx
},
]
}
}
I basically need to grab each author field in the docs[] array, split it by commas to get a new array
$author_array = explode(", ", $author);
I then need to call another web service with these author names that will return JSON. I would love to simply plug this JSON in to my existing JSON and send it all back
Question
I need to modify my JSON so that each doc contains this new JSON object. How can I do this?
{
"title":"xxxx-x-x-xxxx-xxxx",
"author":"author 1, author 2",
"url":"https://xxxxx.com",
"id":xxxxx,
"_version_":xxxxxx,
"authors":[
{
"name": "author1",
"id": "143"
},
{
"name": "author2",
"id": "72"
}
]
}
My Attempt
I have tried a simpler version of this where I simply have an array of author names
{
"title":"xxxx-x-x-xxxx-xxxx",
"author":"author 1, author 2",
"url":"https://xxxxx.com",
"id":xxxxx,
"_version_":xxxxxx,
"authors":[
"author1",
"author2"
]
}
I tried the following:
$response = $obj['response'];
$docs = $response['docs'];
foreach($docs as &$doc) {
$author = $doc['author'];
$authors_array = explode(" ,", $author);
$doc['authors'] = $authors_array;
}
This has ultimately failed as the new array is not appended.

Explode the author string, call the API on each element, make an array of all these results, and put it back into the object.
I use a reference below in foreach so it can modify the document element directly.
foreach ($obj['response']['docs'] AS &$doc) {
$author_names = explode(',', $doc['author']);
$author_array = array_map('author_api', $author_names);
$doc['authors'] = $author_array;
}

Related

Remove the item from array based on child value on object

I've got the following data structure:
Array -> Object -> Array -> Object -> Object
[
{
"id":6834,
"contract_id":13,
"schedule_column_values":[
{
"id":34001,
"field_value":{
"id":324241,
"value":10,
"field":{
"id":1,
"signature":"ios"
}
}
},
{
"id":34001,
"field_value":{
"id":324241,
"value":10,
"field":{
"id":1,
"signature":"android"
}
}
}
]
}
]
What I'm trying to achieve is that if a field has the signature of "android", remove its grandparent object from schedule_column_values. Basically, if a signature is "android", the final data would look like this:
[
{
"id": 6834,
"contract_id": 13,
"schedule_column_values": [
{
"id": 34001,
"field_value": {
"id": 324241,
"value": 10,
"field": {
"id": 1,
"signature": "ios"
}
}
}
]
}
]
This is just an example but the structure is always the same and we always know what signature we're looking for. It could be anything other than android but we know the string we're looking for.
I've tried a nested foreach loop and tried unset but it doesn't seem to work. The other way is I've set a NULL to object value of schedule_column_values when the signature of field is matched, but I cannot have NULL in the object.
What would be a good way to filter out this structure?
This is a perfect use case for array_filter:
$filtered_array = [];
foreach($array as $grandparent){
$filtered_schedules = array_filter(
$grandparent->schedule_column_values,
function($item){
return $item->field_value->field->signature !== 'android';
}
);
$altered_grandparent = clone $grandparent;
$altered_grandparent->schedule_column_values = $filtered_schedules;
$filtered_array[] = $altered_grandparent;
}

Loop json object as array in PHP

How can i loop the response to get all ids and names?
If the response like this:
{
"Data": [
{
"Id": "4321",
"Name": "Dog"
},
{
"Id": "749869",
"Name": "Cat"
}
]
}
I can get all the ids and names using this code:
foreach (json_decode($response->content)->Data as $resp) {
echo $resp->Id;
echo $resp->Name;
}
But, if the response like this:
{
"Data": {
"dog": {
"Id": "4321",
"Name": "Dog"
},
"cat": {
"Id": "749869",
"Name": "Cat"
}
}
}
How can i get all the ids and names without knowing the amount of the array? Thank you.
You can get PHP to read everything into an (associative where applicable) array.
foreach (json_decode($response->content, true)["Data"] as $resp) { //2nd parameter is to decode as array
echo $resp["Id"];
echo $resp["Name"];
}
Example run: https://eval.in/954621
Alternatively you can do the lazy thing and just cast:
foreach ((array)json_decode($response->content)->Data as $resp) {
echo $resp->Id;
echo $resp->Name;
}
Well if you want to extract all ids into one array (I assume you mean all into one array), to use in a database query as an example. I would recommend you doing like this.
$ids = array_values(array_map(function($animal){
return intval($animal['Id']);
}, json_decode($response->content, true)['Data']));
var_dump($ids);
you do the same ! foreach loop would work on both.
the json response supposed to have the same format every time, unless you have a really weird API.
<?php
$response = json_decode($json);
foreach($response as $animal => $data)
{
echo $animal;
echo $data->id;
echo $data->name;
}

How to get json format for monogo db object [duplicate]

I am using PHP to connect with MongoDB. My code is as follows.
// connect
$m = new MongoClient($con_string); // connect to a remote host at a given port
$db = $m->main;
$customers = $db->customer->find();
i want to return $customers collection as json document to my HTML. How can i do this?
You can do this two ways:
echo json_encode(iterator_to_array($customers));
or you can manually scroll through it:
foreach($customers as $k => $row){
echo json_encode($row);
}
Each of MongoDBs objects should have their __toString() methods correctly implemented to bring back the representation of the value.
This also will work. And you can customize your json as well.
$arr = array();
foreach($customers as $c)
{
$temp = array("name" => $c["name"], "phone" => $c["phone"],
"address" => $c["address"]);
array_push($arr, $temp);
}
echo json_encode($arr);
Other answers work, but it is good to know that the generated JSON will have the following form (in this example I use an hypothetical "name" field for your customers):
{
"5587d2c3cd8348455b26feab": {
"_id": {
"$id": "5587d2c3cd8348455b26feab"
},
"name": "Robert"
},
"5587d2c3cd8348455b26feac": {
"_id": {
"$id": "5587d2c3cd8348455b26feac"
},
"name": "John"
}
}
So in case you don't want the Object _id to be the key of each of your result objects you can add a false parameter to iterator_to_array.
Your code would be:
echo json_encode(iterator_to_array($customers, false), true);
This creates the same result as
$result = Array();
foreach ($customers as $entry) {
array_push($result, $entry);
}
echo json_encode($result, true);
which is an array of JSON objects
[
{
"_id": {
"$id": "5587d2c3cd8348455b26feab"
},
"name": "Robert"
},
{
"_id": {
"$id": "5587d2c3cd8348455b26feac"
},
"name": "John"
}
]

parse.com nested objects _encode() and decode to php array not working properly

I have a ParseObject called Follow like explained in this JS tutorial
https://parse.com/docs/js/guide#relations-using-join-tables
Although I'm doing it in PHP. The goal is once a Follow object is saved be able to get a php array representation of the object with the nested classes. The from and to properties of the Follow object are pointers to the _User object.
This block of code create a follow object, then attempts to convert to object to a php array using json_decode, but only the top level object is correctly decoded.
public function post_follow() {
try {
$user = $this->get_user(Input::post('objectId'));
$follow = new ParseObject("Follow");
$follow->set("from", $this->currentUser);
$follow->set("to", $user);
$follow->save();
$this->output = json_decode($follow->_encode(),true);
}
catch(ParseException $ex) {
return $this->error($ex);
}
}
where $this->output is an php array that is later turned back into json via the framework.
This method has the output
{
"objectId": "6Gb1WflPfw",
"createdAt": {
"date": "2015-08-16 22:29:48.445000",
"timezone_type": 2,
"timezone": "Z"
},
"updatedAt": {
"date": "2015-08-16 22:29:48.445000",
"timezone_type": 2,
"timezone": "Z"
},
"from": "{\"objectId\":\"88D437QDxp\",\"createdAt\":{\"date\":\"2015-08-13 08:26:29.478000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"updatedAt\":{\"date\":\"2015-08-16 20:09:17.048000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"email\":\"brian#mail.com\",\"emailVerified\":true,\"followersCount\":1,\"followingCount\":2,\"friendsCount\":18,\"phone\":null,\"username\":\"brian\"}",
"to": "{\"objectId\":\"cmX8o9sEDh\",\"createdAt\":{\"date\":\"2015-08-13 08:29:54.735000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"updatedAt\":{\"date\":\"2015-08-13 08:30:17.188000\",\"timezone_type\":2,\"timezone\":\"Z\"},\"email\":\"brian+2#mail.com\",\"emailVerified\":true,\"phone\":null,\"username\":\"brian2\"}"
}
As you can see the from and to fields are just string literals and not decoded into a php array.
Try this if it works.
public function post_follow() {
try {
$user = $this->get_user(Input::post('objectId'));
$follow = new ParseObject("Follow");
$follow->set("from", $this->currentUser);
$follow->set("to", $user);
$follow->save();
$this->output = json_decode($follow->_encode()); // add true in the second parameter. $this->output = json_decode($follow->_encode(), true);
}
catch(ParseException $ex) {
return $this->error($ex);
}
}

Trying to fetch Json Result

I am trying to fetch the individual values from the obtained json result
{
"_total": 1,
"values": [{
"id": 123456,
"name": "Example Technologies "
}]
}
Now, I need to get the _total value. For that i am using
echo $res->_total;
which gives me
Notice: Trying to get property of non-object
If i try like
echo $res['_total'];
gives me
Warning: Illegal string offset '_total'
So, In what way, I can get the _total value.
Please help me in that. Thanks in advance!
Do this:
$obj = json_decode($res);
echo $obj->_total;
You need to decode the JSON data.
suppose data is
$data = '{"category_id":"10","username":"agent1","password":"82d1b085f2868f7834ebe1fe7a2c3aad:fG"}';
and you want to get particular parameter then
$obj = json_decode($data);
after
$obj->{'category_id'} , $obj->{'username'} , $obj->{'password'}
May be this help you !
It seems that you didn't json_decode() the JSON string, or $res is not the result of json_decode().
Example:
$json = '{
"_total": 1,
"values": [{
"id": 123456,
"name": "Example Technologies "
}]
}';
$res = json_decode($json);
echo $res->_total;
you will need to run the string through json_decode first http://uk3.php.net/json_decode
which will return an array.
Here is Your String,
$data = '{ "_total": 1, "values": [{ "id": 123456, "name": "Example Technologies " }] }';
$test = (array)json_decode($data);
echo '<pre>';
print_r(objectToArray($test));
die;
Function is here
function objectToArray($d) {
if (is_object($d)) {
// Gets the properties of the given object
// with get_object_vars function
$d = get_object_vars($d);
}
if (is_array($d)) {
/*
* Return array converted to object
* Using __FUNCTION__ (Magic constant)
* for recursive call
*/
return array_map(__FUNCTION__, $d);
}
else {
// Return array
return $d;
}
}
May be its help you!!

Categories