0

as usual I'm working with my messed up database and I found that there are some fuxked up JSON string in the database... Some of the JSON strings are missing the double-quotes around the keys and/or values.

I'm thinking of writing a PHP script to fix that, what recommendations do you guys have about how I can detect the missing quotes and automatically add them? Or there is no such convenient way...

Thanks smart community!

4
  • 1
    PEARs JSON class can parse such Javascript expressions, as can upgradephp; though not for values. Conversion approaches have been covered in previous questions on that very topic. Commented Mar 29, 2012 at 22:15
  • @mario Thank you for the information, but does it handle this kind of missing quotes situation? Problem is that some of the JSON strings are not well formed... Anyways I will do a search on other similar questions and see what I find... Commented Mar 29, 2012 at 22:43
  • 1
    Yes, it does handle missing key quotes. But no, completely garbled JSON cannot be reconstructed. You would need a resilent regex for that. (= No. Too much effort.) Commented Mar 29, 2012 at 22:45
  • @mario Hmm that sounds very interesting... I'm curious in how it can handle the missing quotes if the JSON objects are nested... I have already fixed my problem through a very specific detection mechanism, since the error cases are contained. Thank you for your knowledge :) Commented Apr 2, 2012 at 17:06

1 Answer 1

1

You can use json_decode and json_last_error to detect if there is an error in the string. And then json_encode to write the valid json value back out.

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

2 Comments

Thanks for your reply, however I don't see how the json_last_error would help me though. I know there will be an error, but fixing it is the main issue.
Well I have already fixed the problem, luckily the number of error cases is contained so I can just do a very specific detection... Thanks for your reply, and I'll accept your answer to appreciate your help :)

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.