The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

WWW::Metaweb::JSONLikePerl - Access a JSON string like a Perl structure

SYNOPSIS

  use strict;
  use WWW::Metaweb::JSONLikePerl qw(:standard);

  my json = qq({
        "cover_appearances": [
          {
            "part_of_series": "Runaways", 
            "type": "/comic_books/comic_book_issue", 
            "name": "Runaways Vol 1 #1"
          }, 
          {
            "part_of_series": "Runaways", 
            "type": "/comic_books/comic_book_issue", 
            "name": "Runaways Vol. 2 #1"
          }, 
          {
            "part_of_series": "Mystic Arcana", 
            "type": "/comic_books/comic_book_issue", 
            "name": "Mystic Arcana Book IV: Fire"
          }
        ], 
        "name": "Nico Minoru", 
        "created_by": ["Brian K. Vaughan"], 
        "/type/object/creator": "/user/metaweb", 
        "type": "/comic_books/comic_book_character", 
        "id": "/topic/en/nico_minoru"
  });

  my $id = $jsonlp_fetch('->{id}', $json);

  my new_json;

  $new_json = jsonlp_replace('->{cover_appearances}->[2]->{name}', $json, 'Mystic Arcana IV: Sister Grimm');

  $new_json = jsonlp_insert('->{created_by}', $json, 'Adrian Alphona');

  my $second_json = qq({
          "query":{
                  "country":null,
                  "name":99507,
                  "type":["/location/postal_code"]
          }
  });

  $new_json = jsonlp_quote('->{query}->{name}', $second_json, '"');

  $new_json = jsonlp_unquote('->{query}->{type}', $second_json);

ABSTRACT

WWW::Metaweb::JSONLikePerl allows manipulation of a JSON string, referencing items like a perl structure, but without actually converting the string.

EXPORTABLE FUNCTIONS

$value = jsonlp_fetch($structure_path, $json_string, [include_quotes])

Returns the value of the item in $json_string pointed to by $structure_path.

If include_quotes is true then whatever may be quoting the value being fetched will also be included, this may be '{ }' for a hash, '[ ]' for an array, '" "' for a string or make no difference if it's a number or bare word.

$new_json = jsonlp_replace($structure_path, $json_string, $replacement_value)

Replaces the specified JSON node with $replacement_value.

$new_json = jsonlp_insert($structure_path, $json_string, $text_to_insert)

Inserts $text_to_insert into the specified JSON node.

$new_json = jsonlp_quote($structure_path, $json_string, $quote_characters)

Quotes the specified node as specified by $quote_characters.

If $quote_characters has a length of 1 (such as '"') the specified node will be surrounded by that character (eg. "994002"). If it has a length of 2 (such as '{}') the first character will go before the specified node, the second character will go acter the specified node (eg. {994002}). Any other number o characters and undef will be returned.

$new_json = jsonlp_unquote($structure_path, $json_string)

Removes quotes from the specified node.

BUGS AND TODO

None of either as of yet.

ACCKNOWLEDGEMENTS

Mainly the Barcelona weather for keeping me up late enough to come up with this crazy idea.

SEE ALSO

JSON, WWW::Metaweb

AUTHORS

Hayden Stainsby <hds@cpan.org>

COPYRIGHT AND LICENSE

Copyright (C) 2007 by Hayden Stainsby

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.