Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Ambiguous error message from JSON::Path

by Bloehdian (Beadle)
on Apr 16, 2018 at 07:16 UTC ( [id://1212967]=perlquestion: print w/replies, xml ) Need Help??

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

Hey monks,

I have trouble to get the following code to run:

use JSON::Path; $json = q( { "array": [ 1, 2, 3], "string": "string_value", "object": +{ "obj1": "val1", "obj2": "val2" } } ); $arr_path = JSON::Path->new( q($.array) ); $scal_path = JSON::Path->new( q($.string) ); $value = $arr_path->value(); if ( ! ( defined ref $value ) ) { print "SCALAR\n" } elsif ( ref $value eq q(ARRAY) ) { print "ARRAY\n" } else { print "NEITHER" }

I get the following error message:

Use of uninitialized value $json_object in concatenation (.) or string + at /usr/local/share/perl/5.20.2/JSON/Path/Evaluator.pm line 84. Unable to decode as JSON: malformed JSON string, neither tag, array, +object, number, string or atom, at character offset 0 (before "(end o +f string)") at /usr/local/share/perl/5.20.2/JSON/Path/Evaluator.pm li +ne 81. at /usr/local/share/perl/5.20.2/JSON/Path/Evaluator.pm line 79.

According to JSONLint $json is valid JSON. Therefore I do not understand the error message.

What's goin' wrong?

Replies are listed 'Best First'.
Re: Ambiguous error message from JSON::Path
by choroba (Cardinal) on Apr 16, 2018 at 07:31 UTC
    Where do you pass $json to JSON::Path? The documentation seems to pass the json string to the value method.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Ambiguous error message from JSON::Path
by Discipulus (Canon) on Apr 16, 2018 at 07:36 UTC
    Hello,

    it seems you need my $value = $arr_path->value(  $json  ); instead of $value = $arr_path->value();

    I also added use strict and use warnings ..

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1212967]
Approved by Discipulus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 17:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found