Thursday, March 9, 2017

presumed difference in php versions causing difference in JSON output - quotes around integers

I have a testing server with PHP version 5.5.9, and we have a QA server with PHP version 5.6.16.

I am for the sake of this post using the same code branch on both instances (full disclosure: it's a Laravel install but I'm not sure this is a Laravel issue)

On the QA server, an API returns JSON with the integers unquoted as follows (this is simplified):

{["id":1,"name":"John"],["id":2,"name":"Sam"]}

But on my server (the 5.6.16 one), the same exact API call returns:

{["id":"1","name":"John"],["id":"2","name":"Sam"]}

And, Angular is not liking that and does not parse it.

It's not the data

The data and tables the data is coming from are exactly alike in structure - I have verified this.

I don't want to "fix" Angular

Though in theory Angular should handle this(?), I don't want to touch our coding for the front end.

I want the output to be the same..

Regardless of what Angular is doing, the obvious solution is to have the outputs be exactly equal.

What do I do?

  • I want to avoid upgrading PHP on my server - that might open up more problems
  • I am thinking that there is a setting in json_encode() which can be set to change this but I do not know which one.
  • Also there may be a setting in laravel itself which could be at least locally changed on my end. For the record there are well over 100+ json_encode calls in the codebase.


via Oliver Williams

Advertisement