0

In a JSON file I'd like to decode with PHP's json_decode() function I have items like this:

[{
  "id_regione":3,  
  "regione":"Calabria",
  "superficie":15221.9, 
  "num_residenti":1970521,
  "num_comuni":409,
  "num_provincie":5,
  "presidente":"Gerardo Mario Oliverio",
  "cod_istat":18,
  "cod_fiscale":02205340793, /* problem here */
  "piva":null,
  "pec":"dipartimento.presidenza@pec.regione.calabria.it",
  "sito":"www.regione.calabria.it",
  "sede":"Regione Calabria / Via Massara 2, 88100 Catanzaro"
}]

You see, the part "cod_fiscale":02205340793 stops decoding with no apparent reason: but looking at PHP docs I found this in the changelog:

7.0.0 Rejected RFC 7159 incompatible number formats - top level (07, 0xff, .1, -.1) and all levels ([1.], [1.e1])

Oh, I see: 02205340793 seems an incompatible number format... Really it's not a number, it's a tax code (and I could potentially have problems also with phone numbers like 0935123456)

So, what's the best way to deal with these items? Convert everything to a string? And how?

Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
Ivan
  • 2,463
  • 6
  • 39
  • 51
  • 2
    Yes, since those numbers start with a 0, it's best to convert them to a string instead. – aynber May 30 '18 at 14:33
  • 1
    *"You see, the part "cod_fiscale":02205340793 stops decoding with no apparent reason"* There's a clear reason: The JSON is invalid, numbers can only start with a `0` if it's followed by `.`. See the syntax for numbers at http://json.org/. – T.J. Crowder May 30 '18 at 14:34
  • 1
    In case you weren't aware, some systems (including both JS and PHP in some contexts) treat a leading zero as indicating an Octal (base-8) number. – IMSoP May 30 '18 at 14:34
  • 1
    Yes, convert to string. You can use [`sprintf`](http://php.net/manual/en/function.sprintf.php) or whatever else you feel like using. As @IMSoP said, they're treated as octal, which is *soooo* intuitive. – Andrei May 30 '18 at 14:36
  • yes, you should convert it to strings. How to do? Give the json already correctly formatted. – alpham8 May 30 '18 at 14:36
  • 1
    Possible duplicate of [Why is JSON invalid if an integer begins with 0](https://stackoverflow.com/questions/27361565/why-is-json-invalid-if-an-integer-begins-with-0) – iainn May 30 '18 at 14:37
  • 1
    @T.J.Crowder The "no apparent reason" probably refers to the fact that `json_decode` doesn't raise any warnings, instead you have to call [`json_last_error`](http://php.net/manual/en/function.json-last-error.php) to find out what went wrong, which while potentially more useful, is uncommon in PHP, so people don't always know where to look. – IMSoP May 30 '18 at 14:37
  • Try this: `$data = json_decode($json,true, 512, JSON_BIGINT_AS_STRING);` – Markus Zeller May 30 '18 at 14:40
  • @IMSoP - Ew. :-) Thanks. – T.J. Crowder May 30 '18 at 14:42
  • Thanks everyone for the answers: sadly I can't do a manual number -to-string conversion because there are hundreds of items like the one I mentioned (anyway I yet wrote to JSON provider to warn for this problem) Found the problem, the question remains: how to rapidly convert these "numbers" into strings given the whole JSON, please? – Ivan May 30 '18 at 14:55

0 Answers0