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

This node falls below the community's threshold of quality. You may see it by logging in.
  • Comment on How can we retrieve encode_json value which is in encode_json associative array formate.
  • Download Code

Replies are listed 'Best First'.
Re: How can we retrieve encode_json value which is in encode_json associative array formate.
by Corion (Patriarch) on Mar 06, 2011 at 08:14 UTC

    It's very unclear to me where your problem is and what you are trying to do. Your code does also not run by itself, as it does not show encode_json and decode_json.

    I'm not sure whether you want to know how to produce JSON strings or decode them. The easiest way is to use JSON::Any and JSON::XS.

    If you want to know how to use the data structures returned by JSON, see perlreftut and perldsc, and maybe References Quick Reference.

    If your problem is elsewhere, please restate it so it gets clearer to me.

      I want to send  a associative array as a value in json array.



      for example i create a array like.


      %responce_array1=();

      $responce_array1{'REQUEST'}{'Items'{0}{'ItemName'}='demoName0';
      $responce_array1{'REQUEST'}{'Items'}{1}{'ItemName'}='demoName1';
      $responce_array1{'REQUEST'}{'Items'}{2}{'ItemName'}='demoName2';


      my $itemjson_ary1 = encode_json \%responce_array1;


      and pass it to as a value of a another array like :-

      %responce_array2=();

      $responce_array2{'item_json_array'}=$itemjson_ary1;

      and json encode it like :-

      my $itemjson_ary2 = encode_json \%responce_array2;

      it gives out put  as below json encoded array.:-


      print $itemjson_ary2;

      $jsonary='{"item_json_array":"{\"REQUEST\":{\"Items\":{\"1\":{\"ItemName\":\"demoName1\"},\"0\":{\"ItemName\":\"demoName0\"},\"2\":{\"ItemName\":\"demoName2\"}}}}"}';


      Now the problem is  how can  i retrive "ItemName" value.

      because for  example i  take  three values  like 0,1,2 but these can be  many 0,1,2,3,4...

      so i want to retrieve all these value in a loop.or any  way to convert it in to array so we can get these array.

      my whole code as follows.


      use JSON;

      %responce_array1=();
      %responce_array2=();


      $responce_array1{'REQUEST'}{'Items'{0}{'ItemName'}='demoName0';
      $responce_array1{'REQUEST'}{'Items'}{1}{'ItemName'}='demoName1';
      $responce_array1{'REQUEST'}{'Items'}{2}{'ItemName'}='demoName2';

      my $itemjson_ary1 = encode_json \%responce_array1;

      $responce_array2{'item_json_array'}=$itemjson_ary1;

      my $itemjson_ary2 = encode_json \%responce_array2;

      print $itemjson_ary2;

      $jsonary='{"item_json_array":"{\"REQUEST\":{\"Items\":{\"1\":{\"ItemName\":\"demoName1\"},\"0\":{\"ItemName\":\"demoName0\"},\"2\":{\"ItemName\":\"demoName2\"}}}}"}';


      #here  i am decode these json array to get "item_json_array" value .

      $dec_json_obj = decode_json $jsonary;
      $temp=$dec_json_obj->{item_json_array};



      #here  i am decode these json array to get "REQUEST" value .

      $dec_json_obj = decode_json $temp;
      $temp=$dec_json_obj->{REQUEST}; #hash form
      $dec_json_obj=&get_hash_to_str($temp);



      #here  i am decode these json array to get "Items" value .
      $temp=$dec_json_obj->{Items};
      $dec_json_obj=&get_hash_to_str($temp);


      #here  i am decode these json array to get "0" value .
      $temp=$dec_json_obj->{0};
      $dec_json_obj=&get_hash_to_str($temp);


      #here  i am decode these json array to get "ItemName" value .
      print $temp=$dec_json_obj->{ItemName};

      #out put is demoName0

      # its subroutine to convert hash to string

      sub get_hash_to_str($temp)
      {
      my $temp=$_[0];
      my $enc_json_req = encode_json $temp;
      print"
      ";
      print $enc_json_req;
      print"
      ";
      my $dec_json_obj = decode_json $enc_json_req;
      return $dec_json_obj;
      }

        Is that large amount of code necessary to ask how to iterate over an array that you only have a reference to?

        Please read perlreftut and/or References Quick Reference. They explain about references and how to iterate over array references. Also see Data::Dumper for printing out the data structure you have.

        Most likely you just want:

        use Data::Dumper; for my $key (keys %{ $dec_json_obj->{item_json_array}->{REQUEST}->{Ite +ms} }) { print "Key: $key\n"; print "Value: " . Dumper $dec_json_obj->{item_json_array}->{REQUES +T}->{Items}->{ $key }; };
Re: How can we retrieve encode_json value which is in encode_json associative array formate.
by apl (Monsignor) on Mar 06, 2011 at 13:42 UTC
    You might also want to wrap your code in <code> and </code> tags so the rest of us can more easily understand it...

    Modified: to correct the HTML. Thanks AnomalousMonk

Re: How can we retrieve encode_json value which is in encode_json associative array formate.
by TomDLux (Vicar) on Mar 06, 2011 at 19:25 UTC

    I fed your blob of code into perltidy, which complained about certain errors. I made minimal changes to satisfy perltidy, and wound up with the following:

    %responce_array1 = (); %responce_array2 = (); $responce_array1{'REQUEST'}{'Items'}{0}{'ItemName'} = 'demoName0'; $responce_array1{'REQUEST'}{'Items'}{1}{'ItemName'} = 'demoName1'; $responce_array1{'REQUEST'}{'Items'}{2}{'ItemName'} = 'demoName2'; my $itemjson_ary1 = encode_json \%responce_array1; $responce_array2{'item_json_array'} = $itemjson_ary1; my $itemjson_ary2 = encode_json \%responce_array2; print $itemjson_ary2; $jsonary = '{"item_json_array":"{\"REQUEST\":{\"Items\":{\"1\":{\"ItemName\": +\"demoName1\"},\"0\":{\"ItemName\":\"demoName0\"},\"2\":{\"ItemName\" +:\"demoName2\"}}}}"}'; $dec_json_obj = decode_json $jsonary; $temp = $dec_json_obj->{item_json_array}; $dec_json_obj = decode_json $temp; $temp = $dec_json_obj->{REQUEST}; #hash form $dec_json_obj=&get_hash_to_str($temp); $temp = $dec_json_obj->{Items}; $dec_json_obj = &get_hash_to_str($temp); $temp = $dec_json_obj->{0}; $dec_json_obj = &get_hash_to_str($temp); print $temp= $dec_json_obj->{ItemName}; sub get_hash_to_str($temp) { my $temp = $_[0]; my $enc_json_req = encode_json $temp; print " "; print $enc_json_req; print " "; my $dec_json_obj = decode_json $enc_json_req; return $dec_json_obj; } # Here i am sending values like these to retrieve # 0-th element of item and get ItemName # $temp = $dec_json_obj->{0}; $dec_json_obj = &get_hash_to_str($temp); print $temp= $dec_json_obj->{ItemName};

    I've made some minor spelling corrections.

    • Declaring a new array automaticall makes it empty; declaring a new hash automatically makes it empty. You only need to initialize it to () if it previously had contents and you want to discard those old values.
    • A variable with a % in front is a hash, not an array.
    • You don't need to quote hash keys, unless they contain spaces or other characters not acceptable in a bareword hash key. My philosophy is to always use warnings and strict, and the computer will tell me when I have a dubious key value that needs quotes.
    • If you have a hash with values {0], {1}, {2}, you should probably be using an array.
    • Using ampersand (&) on subroutine calls used to be correct in Perl4 ... 1993 ... but is usually wrong in Perl5. If you have &, you should almost never have any arguments.
    • Do you really want to have print $temp = ...; i.e., printing the return value of the assignment? The result is the same as $temp = ...; print $temp, but it's irregular, unusual, requires thinking about rather than being standard.
    • In a subroutine, the normal way to set arguments is:my ( $var1, $var2 ) = @_;.
    • The few print statements are odd ... looks like maybe they are an attempt at debugging?
    • All variables are temporary; try using names relevant to the problem you are trying to solve, rather than relevant to the programming language solution you are implementing.
    • I gave up on trying to figure out what that string thing in the middle was, with all the escaped double quotes. I mean, I can see you are trying to build up an encoded string representation of some object, but it is ugly, and difficult to verify as being right or wrong. I would suggest building up small pieces, and assembling them together. given the availability of single quote, double quote, qw{}, q{}, and qq{}, it shouldn't be necessary to escape quotes in strings.
    my ( %response1, %response2 ); $response1{REQUEST}{Items}[0]{ItemName} = 'demoName0'; $response1{REQUEST}{Items}[1]{ItemName} = 'demoName1'; $response1{REQUEST}{Items}[2]{ItemName} = 'demoName2'; my $itemjson_ary1 = encode_json \%response1; $response2{item_json_array} = $itemjson_ary1; my $itemjson_ary2 = encode_json \%resposce2; print $itemjson_ary2; $jsonary = '{"item_json_array":"{\"REQUEST\":{\"Items\":{\"1\":{\"ItemName\": +\"demoName1\"},\"0\":{\"ItemName\":\"demoName0\"},\"2\":{\"ItemName\" +:\"demoName2\"}}}}"}'; $dec_json_obj = decode_json $jsonary; $temp = $dec_json_obj->{item_json_array}; $dec_json_obj = decode_json $temp; $temp = $dec_json_obj->{REQUEST}; # hash form $dec_json_obj = get_hash_to_str($temp); $temp = $dec_json_obj->{Items}; $dec_json_obj = get_hash_to_str($temp); $temp = $dec_json_obj->{0}; $dec_json_obj = get_hash_to_str($temp); print $temp = $dec_json_obj->{ItemName}; sub get_hash_to_str($temp) { my ( $stringify_me ) = @_; my $enc_json_req = encode_json $stringify_me; print " "; print $enc_json_req; print " "; my $dec_json_obj = decode_json $enc_json_req; return $dec_json_obj; } # Here i am sending values like these to retrieve # 0-th element of item and get ItemName # $temp = $dec_json_obj->[0]; $dec_json_obj = get_hash_to_str($temp); print $temp = $dec_json_obj->{ItemName};

    Hope this helps a little.

    As Occam said: Entia non sunt multiplicanda praeter necessitatem.