1

I have bunch of json files to read through and store some information to db using PHP.

The problem is these json keys are without quotes like:

[{f:{v:11,ib:5,gh:"res",bfr:7,ju:7,ugy:8,ki:"y"...

PHP doesn't recognize it as json. However, if I take this json file and run it in a browser with javascript function "JSON.stringify" the data is formatted with quotes which can be used in PHP.

So I wrote a script to parse these files using javascript and then post it to PHP script to write it to the server. Works well in the browser BUT I cannot automate this as curl does not run javascript!!!

Is there anyway to convert json format (without quotes) to PHP readable format? I tried this suggestion... regex option did not work and I do not know how to work with PEAR, even though my server has PEAR Version: 1.10.1 and PHP Version: 5.4.45

Community
  • 1
  • 1
Jsp
  • 166
  • 1
  • 2
  • 13
  • See [this answer](http://stackoverflow.com/a/40326949/5459839) I added to the question you referred to. It should solve your case as well. – trincot Oct 30 '16 at 16:59
  • Thank you for the regex that you posted for my question, it works well BUT integers with leading zero like this: "code":012345 will not json_decode in PHP. Can the regex be modified to make integers leading with zero to be quoted? – Jsp Nov 01 '16 at 16:44
  • I see what you mean, but it they are integers in the JavaScript notation, they should also be integers in the JSON to be consistent, so I would suggest a solution where the non-necessary pre-padded zeroes are removed. If you find that acceptable, I will add such a solution, but otherwise (if it needs to be quoted), I suggest you ask a new question, because such is very specific behaviour that does not fit the context of my answer. – trincot Nov 01 '16 at 17:35
  • Sure non-necessary pre-padded zeroes can be removed. – Jsp Nov 01 '16 at 17:38
  • 1
    OK, I added a regex for that as well in [this answer](http://stackoverflow.com/a/40326949/5459839). – trincot Nov 01 '16 at 18:32
  • PERFECT! Thank you! – Jsp Nov 01 '16 at 18:57

1 Answers1

1

Here is my solution to your problem. Yeah it is regex but it works.

$text = preg_replace(["/\\\\'/", '/("(.*?)"|(\w+))(\s*:\s*(".*?"|.))/s'], ["'", '"$2$3"$4'], $text);
$text = json_decode($text);

$text - is the bad formatted json

For the last comment this should work:

$text = preg_replace(["/\\\\'/", '/("(.*?)"|(\w+))(\s*:\s*(".*?"|.))/s', '/((:\s*)(0\d+))/'], ["'", '"$2$3"$4', '$2"$3"'], $text);
krasipenkov
  • 2,031
  • 1
  • 11
  • 13
  • Unreliable! Changes this `"2013-01-05T08:02:01"` to `"2013-01-"05T08":"02":01"` – Jsp Oct 30 '16 at 03:43
  • What about my updated answer? – krasipenkov Oct 30 '16 at 11:12
  • Alright regex guru - it works! BUT I had to `$text = str_replace("\'", "'", $text);` before json_decode. If it can be rolled into regex, it would be perfect one line solution. – Jsp Oct 31 '16 at 02:32
  • 1
    I ran across another glitch when the values had `:` like `"smile please:)"` which becomes `"smile "please":)"`. trincot regex worked. This is the working code: `$text = preg_replace(["/\\\\'/", '/("(.*?)"|(\w+))(\s*:\s*(".*?"|.))/s'], ["'", '"$2$3"$4'], $text);` – Jsp Nov 01 '16 at 15:16
  • Another gotcha - integers with leading zero like this: "code":012345 will not json_decode in PHP. Can the regex be modified to make integers leading with zero to be quoted? – Jsp Nov 01 '16 at 16:56
  • Check my updated answer – krasipenkov Nov 02 '16 at 21:14