


JSON_UNESCAPED_SLASHES – It prevents escaping slashes (/).JSON_PRETTY_PRINT – pretty print is used for adding white space with the JSON formatted data.JSON_BIGINT_AS_STRING – This constant is used to convert the log integer value as a string.JSON_NUMERIC_CHECK – PHP JSON encode function will return numbers as a result of encoding given a number with the string data type.JSON_FORCE_OBJECT – Using this, json_encode will return an object for given input data except for the associative array.JSON_HEX_QUOT – converts double quotes (“) into \u0022.JSON_HEX_APOS – encode apostrophe (‘) with \u0027.JSON_HEX_AMP – Used to encode data by replacing ampersand symbol (&) with \u0026.JSON_HEX_TAG – Used to encode HTML content by replacing symbol with \u003C and \u003E.Depth limit for performing recursive encoding with nested levels of input.įor PHP JSON encodes, the following list of constants will be used for the options parameter of the json_encode() function.Options with JSON encode constants to reflect effects on encoding behavior.This function accepts the following set of arguments. In PHP, json_encode() is used to convert PHP-supported data type into JSON formatted string to be returned as a result of JSON encode operation. Decoding is a reverse process that reverts encoded data back to its original form. This process will be required to preserve data consistency. Previously, we have seen PHP functions url_encode() and url_decode() to perform the encoding and decoding of a given URL.Įncoding is used to bundle data with respect to a particular format. Encoding and DecodingĮncoding and decoding are the pair of operations which is most importantly used in many application programming. Apart from these functions, a complete guide to handling JSON with PHP will be useful for you to know more about PHP JSON. In this article, we are going to learn about these functions with suitable examples. In this article, we are going to see how to encode and decode JSON using PHP. PHP provides built-in functions to perform these two operations. JSON encode decode is one of the most frequently required operations. There is a 5.4 alpha release candidate on QA though if you want to play on your development machine.By Vincy. JSON_UNESCAPED_UNICODE was added in PHP 5.4 so it looks like you need upgrade your version of PHP to take advantage of it. (You could simply use utf8_encode, but this will only support characters that are part of ISO 8859-1).įinally, as another answer noted, you do need to make sure that you're declaring the proper charset, with an HTTP header or otherwise (of course, this particular issue might have just been an artifact of the environment where you did your print_r testing). There's also the possibility that the data currently stored in the database is in the wrong encoding.

The PHP manual indicates you'd do this by appending options='-client_encoding=UTF8' to the connection string. So, it seems like you need to configure your connection to Postgres so that it will give you UTF-8 strings. PHP will use the \uXXXX escaping, but as you noted, this is valid JSON. You said you had the same problem in Python, which would seem to exclude PHP from being the issue.( html_entity_decode, confusingly, defaults to UTF-8, so your method had the effect of converting from ISO 8859-1 to UTF-8.) If you don't specify one, it assumes ISO 8859-1.

htmlentities needs to know what the encoding of the input string to work correctly. Your htmlentities hackaround solution worked.You did say that you got "mangled" output when using print_r after doing utf8_encode, but the mangled output you got is actually exactly what would happen by trying to parse UTF-8 text as ISO 8859-1 (ó is \圆3\xb3 in UTF-8, but that sequence is ó in ISO 8859-1.json_encode produced parseable output after you ran the input string through utf8_encode, which converts from ISO 8859-1 to UTF-8.Judging from everything you've said, it seems like the original Odómetro string you're dealing with is encoded with ISO 8859-1, not UTF-8. json_encode($data,JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES) I have found following way to fix this issue.
