Is it possible to customize the JSON content in PHP?

I have the following JSON data which is basically a result of echo json_encode(get_file_post_data());.

{
	"id": "123",
	"username": "myusername",
	"profileImg": {
		"name": "Document1.pdf",
		"type": "application\/pdf",
		"tmp_name": "\/Applications\/MAMP\/tmp\/php\/phph1E3Jx",
		"error": 0,
		"size": 366875
	},
	"displayImg": {
		"name": "chex.pdf",
		"type": "application\/pdf",
		"tmp_name": "\/Applications\/MAMP\/tmp\/php\/phpguqAEM",
		"error": 0,
		"size": 374985
	}
}

I want to convert it in the following format using PHP :

[{
    "name" : "id",
    "value" : "123"
}, {
   "name" : "username",
   "value" :  "myusername"
}, {
    "name" : "profileImg_name",
    "value" :  "Document1.pdf"
}, {
    "name" : "profileImg_type",
    "value" :  "application\/pd"
}}

Similarly for other parameters also lile temp_name, error and size.

Is it possible to convert it in the above formate using PHP loop?

Yes. What have you tried?

Here’s what I was trying:

For example, I took this simple example :

$myObj->id = "123";
$myObj->username = "people";


$myJSON = json_encode($myObj);

echo $myJSON;

This prints the following on browser:

{"id":"123","username":"people"}

When I tried to print it like this using the loop :

foreach($myJSON as $key => $value) {
  echo "$key is at $value";
}

It didn’t print anything. Also, even if I manage to print it in my desired format, I don’t want to echo it but store all the result in maybe a variable or something so that I could pass it to a webservice using cURL.

strings are not iterable by foreach()

1 Like

Once you have run your object through JSON encoding, it comes out as a single string, which is what makes it useful for sending multiple data items through a medium where field separators and different data types are difficult to handle. So, you either need to use json_decode() to convert it back into the format you started with, or work through it yourself using explode() and so on. The latter option, though, is just writing your own version of json_decode() so probably easier to use the original.

Thanks. When I used

$other = json_decode($myJSON);

echo $other;

I got an error :

Catchable fatal error: Object of class stdClass could not be converted to string in C:\wamp\www\filewor......

Is that because $other is your original object, and you can’t just echo that out? You need to access it as you would normally access the object members. Can you do this?

echo $other->id;
echo $other->username;

I see. Using echo $other->id; printed the 123 which is the id. Then I am wondering should I be building a JSON in the format I mentioned above (containing name and value) using echo? Because here I am not at a point where I could loop an array and use foreach something.

These are three pairs of shoes. JSON is a data interchange format. ECHO is a command to push a string onto the stdout stream. LOOPs are flow control statements. They are not related.

Take a look at the free PHP online manual and notice the additional parameters

http://php.net/manual/en/function.json-decode.php

Edit:
After applying the relevant parameter wrap the print_r($result); inside PRE tags which adds line feeds to the single output string.

echo '<pre>';
  print_r($result); // or var_dump(...);
echo '</pre>';

Ok. Thanks. I think I got what I was looking for after doing something like this :

 $myObj->id = "123";
$myObj->username = "people";

$other = json_decode($myJSON);

$arr = array(
    array(
        "name" => "id",
        "value" => $other->id
    ),
    array(
        "name" => "username",
        "value" => $other->username
    ) 
);

echo json_encode($arr);

This prints :
[{"name":"id","value":"123"},{"name":"username","value":"people"}]

I can probably pass the $arr to my java webservice which is accepting a JSON object.

Just wondering, how can I generate the following so that I can also test it :

"profileImg": {
		"name": "Document1.pdf",
		"type": "application\/pdf",
		"tmp_name": "\/Applications\/MAMP\/tmp\/php\/phph1E3Jx",
		"error": 0,
		"size": 366875
	},

Something like $myObj->profileImg = ????

If you are more familiar with arrays then check the link in post#10. Only a single extra parameter and an array is returned.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.