1

I have this array

$params_array['application_id'] = 'xxxxxxxxx';
$params_array['v'] = 1.20;
$params_array['src'] = 'http://www.google.com/logos/2011/yokoyama11-hp.jpg';
$params_array['functions']['name'] = 'blur';
$params_array['functions']['params']['radius'] = '0.0';
$params_array['functions']['params']['sigma'] = '2.0';
$params_array['functions']['save']['image_identifier'] = 'MY_CLIENT_ID';

I need to transform it into json.

So I am doing this:

$json = json_encode($params_array,JSON_UNESCAPED_SLASHES);

The result is

{"application_id":"xxx","v":1.2,"src":"http://www.google.com/logos/2011/yokoyama11-hp.jpg","functions":{"name":"blur","params":{"radius":"0.0","sigma":"2.0"},"save":{"image_identifier":"MY_CLIENT_ID"}}}

but, the receiver API of that json wants it to be formed slightly different, like this:

{"application_id":"xxx","v":1.2,"src":"http://www.google.com/logos/2011/yokoyama11-hp.jpg","functions":[{"name":"blur","params":{"radius":"0.0","sigma":"2.0"},"save":{"image_identifier":"MY_CLIENT_ID"}}]}

The difference: after "functions": there is this bracket [, and it's closed at the end.

PHP somehow does not create the json with this bracket.

How can I get PHP to create the json with those brackets?

The receiver API is http://www.blitline.com/docs/quickstart

2
  • What is the receiver API? Commented Aug 27, 2014 at 12:30
  • The output from json_encode is correct JSON-syntax. Review your receiver API so that this is correct. Try your different versions here; jsoneditoronline.org Commented Aug 27, 2014 at 12:31

5 Answers 5

2

You should prepare your structure this way:

$params_array['application_id'] = 'xxxxxxxxx';
$params_array['v'] = 1.20;
$params_array['src'] = 'http://www.google.com/logos/2011/yokoyama11-hp.jpg';
$params_array['functions'][0]['name'] = 'blur';
$params_array['functions'][0]['params']['radius'] = '0.0';
$params_array['functions'][0]['params']['sigma'] = '2.0';
$params_array['functions'][0]['save']['image_identifier'] = 'MY_CLIENT_ID';

(Making functions to a number indexed array.)

Sign up to request clarification or add additional context in comments.

Comments

1

So receiver expects functions to be array objects, but you pass single object instead. Change $params_array['functions']['name'] to $params_array['functions'][$functionIndex]['name']

3 Comments

Will do, but stackoverflow says wait 7 minutes
wouldn't this create a new array for every property besides name? params and save should be in the same array as name
Yes, so you have to put explicit index
0

Try this

$params_array['application_id'] = 'xxxxxxxxx';
$params_array['v'] = 1.20;
$params_array['src'] = 'http://www.google.com/logos/2011/yokoyama11-hp.jpg';
$function_array['name'] = 'blur';
$function_array['params']['radius'] = '0.0';
$function_array['params']['sigma'] = '2.0';
$function_array['save']['image_identifier'] = 'MY_CLIENT_ID'; 
$params_array['functions'] = array($function_array);

Reason: JSON_ENCODE will treat an Array as JSON OBJECT if it is like Key=>Value and it will treat as json array if it is index based like [0] => value

Comments

0

Alright, run this example then you'll know how to achieve it.

$params_array = array();
$params_array['application_id'] = 'xxxxxxxxx';
$params_array['another_array'] = array("A","B","C");
echo json_encode($params_array );

Result: {"application_id":"xxxxxxxxx","another_array":["A","B","C"]}

Comments

0

You can try to use the flag JSON_FORCE_OBJECT if you have PHP 5.4.0 or later :

$json = json_encode($params_array,JSON_UNESCAPED_SLASHES + JSON_FORCE_OBJECT);

2 Comments

Notice: Use of undefined constant JSON_UNESCAPED_SLASHES - assumed 'JSON_UNESCAPED_SLASHES'
... In PHP <5.4.0 . This constant, which the author included in his question, is available since PHP 5.4.0. php.net/manual/en/json.constants.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.