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

Hi,

I'm trying to convert this PHP code into JSON:

$return = array( 'status' => '0', 'error' => $error ); echo json_encode($return);


I've tried stuff like this, but it doesn't seem to be the right format:

I've tried all kinda things:

print qq|{ 'status' => '0', 'error' => "BLA" }|;

...and:
print qq|{ "status": '0', "error": 'foo' }|;
...and:
print qq|<response> <status>0</status> <error>foo</error> </response>|;


None of which seem to work.

Anyone got any ideas? (I'm new to JSON btw ;))

TIA

Andy

Replies are listed 'Best First'.
Re: Convert to JSON, without a module?
by moritz (Cardinal) on Sep 20, 2010 at 10:07 UTC
    None of which seem to work.

    That's because neither of your examples produces valid JSON strings. To elaborate, strings in JSON are delimited with double quotes, not with single quotes. See http://json.org/.

    (Still in general "None of which seem to work." isn't very helpful, you should at least say what happens when you try).

    A better approach than blindly (and without looking at the spec) trying stuff would be to either write a minimal JSON implementation of the subset you need, or just copying&pasing code from JSON::PP.

    Update: For developement, I have a small script which uses JSON to check the syntax of JSON files. Even if you don't want to install that module everywhere your script is used, you can use it on your development machine for good effect. Here it goes:

    #!/usr/bin/perl use strict; use warnings; use JSON; die "Usage: $0 <filename>" if @ARGV != 1; open my $h, '<', $ARGV[0] or die "Can't open file '$ARGV[0]' for reading: $!"; my $contents = do { local $/; <$h> }; close $h or warn $!; my $res = from_json($contents); # vim: ft=perl
    Perl 6 - links to (nearly) everything that is Perl 6.
      Thanks, will give that a go :)
Re: Convert to JSON, without a module?
by Anonymous Monk on Sep 20, 2010 at 08:43 UTC
      Yup, I'm aware of that - but I'm trying to do it without extra perl modules (as its only that single line I need to print, as part of a JS script, which expects a JSON response)

      This script is gonna be a "plugin", so I don't want people to have to install extra modules unnecesserily.

      TIA

      Andy
        Hi,

        I was having similar problem with to conversion to json on machine where modules cannot be installed. Here is what I end up with (some pieces copied from JSON::PP):

        sub to_json { my ($ref) = @_; if(ref($ref) eq "HASH") { return "{".join(",",map { "\"$_\":".to_json($ref->{$_}) } sort + keys %$ref)."}"; } elsif(ref($ref) eq "ARRAY") { return "[".join(",",map { to_json($_) } @$ref)."]"; } else { return "null" if ! defined $ref; return $ref if int($ref) eq $ref; my %esc = ( "\n" => '\n', "\r" => '\r', "\t" => '\t', "\f" => '\f', "\b" => '\b', "\"" => '\"', "\\" => '\\\\', "\'" => '\\\'', ); $ref =~ s/([\x22\x5c\n\r\t\f\b])/$esc{$1}/g; $ref =~ s/([\x00-\x08\x0b\x0e-\x1f])/'\\u00' . unpack('H2', $1 +)/eg; return "\"$ref\""; } }

        Just call it with reference

        print to_json({ this => 'that', other => 'whatever' });

        -- hope it helps, Roman

Re: Convert to JSON, without a module?
by Proclus (Beadle) on Sep 20, 2010 at 15:12 UTC
    I don't think adding a JSON module requirement would present a problem for the users. JSON is very popular and becoming a requirement in many projects. I suggest you reevaluate your decision. The project can grow in time and eventually you may end up using a JSON module.