odrevet has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks ! I try to parse the following Json With the sub from_json of the JSON module

{Id:'343410466704417574',Pseudo:"LRYWLL",Country:'PERL_REPUBLIC',CountryId:1,Can:'true'}

But I got the error :
'"' expected, at character offset 1 (before "Id:'3434104667044175...") at C:/str awberry/perl/site/lib/JSON.pm line 154.
I tried to replace ' with " but it don't works...
  • Comment on JSON error '"' expected, at character offset 1

Replies are listed 'Best First'.
Re: JSON error '"' expected, at character offset 1
by Corion (Patriarch) on Jan 01, 2010 at 23:23 UTC

    JSON expects double quotes. So, replacing the double quotes should work. Maybe you care to show us the code you used to replace the single quotes with double quotes?

Re: JSON error '"' expected, at character offset 1
by ikegami (Patriarch) on Jan 02, 2010 at 06:23 UTC
    JSON::PP's with allow_singlequote and allow_barekey should do the trick
      Thanks a lot everyone, both solution works. I'll use  $response =~s/[{:,]\K(\w+)(?=[:,}])/"$1"/g; to limit module usage, but PP is fine.
        What's wrong with using a module, and why would you rather do it wrong? (Your thing won't work if any string contains a double quote, for example.)
Re: JSON error '"' expected, at character offset 1
by Anonymous Monk on Jan 01, 2010 at 23:46 UTC
    That is valid javascript but not valid JSON :) so use some javascript to convert it to JSON :)
    alert( JSON.stringify({Id:'343410466704417574',Pseudo:"LRYWLL",Country +:'PERL_REPUBLIC',CountryId:1,Can:'true'}) )
      Thanks but I can't use JS in my program.
      I Easly replaced every single quotes by double quotes with $response =~ s/'/"/g; Now how can I do with a regex :
      Quote every unquoted characteres beetween { or , or :
      and { or , or :
      Can I capture matching string with ( ) and use it in the replace part of the regex with quote around ?
      I tried with
      $response =~s/({|,|:)(.+)(}|,|:)/"$&"/g;

      But it quotes the whole JSON...

        What about

        quote: 'He said: "boo!"'

        Can I capture matching string with ( ) and use it in the replace part of the regex with quote around ?

        $1

        >perl -le"$_='abc'; s/(.)/[$1]/sg; print" [a][b][c]
        Thanks but I can't use JS in my program.

        Maybe you should insist on JSON :D