Json_Decode Object Remains NULL / Empty - json

I have a json_decoded response that looks like this:
string(664) "{ "ticker": "AAPL:US" }"
Now I am using json_decode with 'true' to access the data as an object:
print_r(json_decode($res));
When trying to dump the content it remains NULL
var_dump($res->ticker);
How would I access the data without an array just the object?
EDIT: It remains 0 when using an array or an object. Same result

There are two major issues with your code:
1) When your second parameter is set to true, returned object will be converted into associative array. You have to set it to false (it's default value anyway).
2) json_decode does not change the value of $res, instead, it will return value encoded in json in appropriate type (mainly object or associative array).
Try this:
$res = '{ "ticker": "AAPL:US" }';
$obj = json_decode( $res, false );
var_dump( $obj );

Related

Perl, JSON parsing values incorrectly

I'm parsing a JSON string that is stored in a database.
{"name":"simon", "age":"23", "height":"tall"}
I'm pulling the data, then decoding. When running the code below, I'm receiving weird 'HASH' values back.
use JSON;
$data = decode_json($row->{'address'});
for my $key (keys %$data){
if($data->{$key} ne ''){
$XML .= " <$key>$data->{$key}</$key>";
}
}
// Returns data like so
<company_type>HASH(0x27dbac0)</company_type>
<county>HASH(0x27db7c0)</county>
<address1>HASH(0x27dba90)</address1>
<company_name>HASH(0x27db808)</company_name>
The Error happens when I have a data set like so:
{"name":"", "age":{}, "height":{}}
I don't understand why JSON / Arrays / Hashes have to be so difficult to work with in Perl. What point am I missing?
You are processing a flat hash, while your data in fact has another, nested, hashref. In the line
{ "name":"", "age":{}, "height":{} }
the {} may be intended to mean "nothing" but are in fact JSON "object", the next level of nested data (which are indeed empty). In Perl we get a hashref for it and that's what your code prints.
The other pillar of JSON is an "array" and in Perl we get an arrayref. And that's that -- decode_json gives us back the top-level hashref, which when dereferenced into a hash may contain further hash or array references as values. If you print the whole structure with Data::Dumper you'll see that.
To negotiate this we have to test each time for a reference. Since a dereferenced hash or array may contain yet further levels (more references), we need to use either a recursive routine (see this post for an example) or a module for complex data structures. But for the first level
for my $key (keys %$data)
{
next if $data->{$key} eq '';
my $ref_type = ref $data->{$key};
# if $data->{key} is not a reference ref() returns an empty string (false)
if (not $ref_type) {
$XML .= " <$key>$data->{$key}</$key>";
}
elsif ($ref_type eq 'HASH') {
# hashref, unpack and parse. it may contain references
say "$_ => $data->{$key}{$_}" for keys %{ $data->{$key} };
}
elsif ($ref_type eq 'ARRAY') {
# arrayref, unpack and parse. it may contain references
say "#{$data->{$key}}";
}
else { say "Reference is to type: $ref_type" }
}
If the argument of ref is not a reference (but a string or a number) ref returns an empty string, which evaluates as false, which is when you have plain data. Otherwise it returns the type the reference is to. Coming from JSON it can be either a HASH or an ARRAY. This is how nesting is accomplished.
In the shown example you are runnig into hashref. Since the ones you show are empty you can just discard them and the code for the specific example can reduce greatly, to one statement. However, I'd leave the other tests in place. This should also work as it stands with the posted example.

Encoding an array of hashes in Perl

I'm trying to do something that seems to be very simple, but I can't figure out how to do it in Perl : I want to output a JSON-formatted array of hashes.
The array of hashes in question is actually an array of DBIx::MyParse Items object instances. Here is my code :
use strict;
use DBIx::MyParse;
use JSON::PP;
my $json = JSON::PP->new->ascii->pretty->allow_nonref;
our $parser = DBIx::MyParse->new( database => "test", datadir => "/tmp/myparse" );
our $query = $parser->parse("UPDATE table1 SET field1 = 1;");
$json->convert_blessed(1);
print $json->encode(#{$query} );
And this is what this script outputs :
"SQLCOM_UPDATE"
Which is actually the first element of the array that I want to output as a whole. Here is the content of the array that I see when I step-by-step debug the script :
I would like to have the whole structure in my JSON output. How can I achieve this ?
JSON::encode just expects a single argument, not a list. Use $json->encode( $query ).

Decoding and using JSON data in Perl

I am confused about accessing the contents of some JSON data that I have decoded. Here is an example
I don't understand why this solution works and my own does not. My questions are rephrased below
my $json_raw = getJSON();
my $content = decode_json($json_raw);
print Data::Dumper($content);
At this point my JSON data has been transformed into this
$VAR1 = { 'items' => [ 1, 2, 3, 4 ] };
My guess tells me that, once decoded, the object will be a hash with one element that has the key items and an array reference as the value.
$content{'items'}[0]
where $content{'items'} would obtain the array reference, and the outer $...[0] would access the first element in the array and interpret it as a scalar. However this does not work. I get an error message use of uninitialized value [...]
However, the following does work:
$content->{items}[0]
where $content->{items} yields the array reference and [0] accesses the first element of that array.
Questions
Why does $content{'items'} not return an array reference? I even tried #{content{'items'}}, thinking that, once I got the value from content{'items'}, it would need to be interpreted as an array. But still, I receive the uninitialized array reference.
How can I access the array reference without using the arrow operator?
Beginner's answer to beginner :) Sure not as profesional as should be, but maybe helps you.
use strict; #use this all times
use warnings; #this too - helps a lot!
use JSON;
my $json_str = ' { "items" : [ 1, 2, 3, 4 ] } ';
my $content = decode_json($json_str);
You wrote:
My guess tells me that, once decoded, the object will be a hash with
one element that has the key items and an array reference as the value.
Yes, it is a hash, but the the decode_json returns a reference, in this case, the reference to hash. (from the docs)
expects an UTF-8 (binary) string and tries to parse that
as an UTF-8 encoded JSON text,
returning the resulting reference.
In the line
my $content = decode_json($json_str);
you assigning to an SCALAR variable (not to hash).
Because you know: it is a reference, you can do the next:
printf "reftype:%s\n", ref($content);
#print: reftype:HASH ^
#therefore the +------- is a SCALAR value containing a reference to hash
It is a hashref - you can dump all keys
print "key: $_\n" for keys %{$content}; #or in short %$content
#prints: key: items
also you can assing the value of the "items" (arrayref) to an scalar variable
my $aref = $content->{items}; #$hashref->{key}
#or
#my $aref = ${$content}{items}; #$hash{key}
but NOT
#my $aref = $content{items}; #throws error if "use strict;"
#Global symbol "%content" requires explicit package name at script.pl line 20.
The $content{item} is requesting a value from the hash %content and you never defined/assigned such variable. the $content is an scalar variable not hash variable %content.
{
#in perl 5.20 you can also
use 5.020;
use experimental 'postderef';
print "key-postderef: $_\n" for keys $content->%*;
}
Now step deeper - to the arrayref - again you can print out the reference type
printf "reftype:%s\n", ref($aref);
#reftype:ARRAY
print all elements of array
print "arr-item: $_\n" for #{$aref};
but again NOT
#print "$_\n" for #aref;
#dies: Global symbol "#aref" requires explicit package name at script.pl line 37.
{
#in perl 5.20 you can also
use 5.020;
use experimental 'postderef';
print "aref-postderef: $_\n" for $aref->#*;
}
Here is an simple rule:
my #arr; #array variable
my $arr_ref = \#arr; #scalar - containing a reference to #arr
#{$arr_ref} is the same as #arr
^^^^^^^^^^ - array reference in curly brackets
If you have an $arrayref - use the #{$array_ref} everywhere you want use the array.
my %hash; #hash variable
my $hash_ref = \%hash; #scalar - containing a reference to %hash
%{$hash_ref} is the same as %hash
^^^^^^^^^^^ - hash reference in curly brackets
If you have an $hash_ref - use the %{$hash_ref} everywhere you want use the hash.
For the whole structure, the following
say $content->{items}->[0];
say $content->{items}[0];
say ${$content}{items}->[0];
say ${$content}{items}[0];
say ${$content->{items}}[0];
say ${${$content}{items}}[0];
prints the same value 1.
$content is a hash reference, so you always need to use an arrow to access its contents. $content{items} would refer to a %content hash, which you don't have. That's where you're getting that "use of uninitialized value" error from.
I actually asked a similar question here
The answer:
In Perl, a function can only really return a scalar or a list.
Since hashes can be initialized or assigned from lists (e.g. %foo = (a => 1, b => 2)), I guess you're asking why json_decode returns something like { a => 1, b => 2 } (a reference to an anonymous hash) rather than (a => 1, b => 2) (a list that can be copied into a hash).
I can think of a few good reasons for this:
in Perl, an array or hash always contains scalars. So in something like { "a": { "b": 3 } }, the { "b": 3 } part has to be a scalar; and for consistency, it makes sense for the whole thing to be a scalar in the same way.
if the hash is quite large (many keys at top-level), it's pointless and expensive to iterate over all the elements to convert it into a list, and then build a new hash from that list.
in JSON, the top-level element can be either an object (= Perl hash) or an array (= Perl array). If json_decode returned a list in the former case, it's not clear what it would return in the latter case. After decoding the JSON string, how could you examine the result to know what to do with it? (And it wouldn't be safe to write %foo = json_decode(...) unless you already knew that you had a hash.) So json_decode's behavior works better for any general-purpose library code that has to use it without already knowing very much about the data it's working with.
I have to wonder exactly what you passed as an array to json_decode, because my results differ from yours.
#!/usr/bin/perl
use JSON qw (decode_json);
use Data::Dumper;
my $json = '["1", "2", "3", "4"]';
my $fromJSON = decode_json($json);
print Dumper($fromJSON);
The result is $VAR1 = [ '1', '2', '3', '4' ];
Which is an array ref, where your result is a hash ref
So did you pass in a hash with element items which was a reference to an array?
In my example you would get the array by doing
my #array = #{ $fromJSON };
In yours
my #array = #{ $content->{'items'} }
I don't understand why you dislike the arrow operator so much!
The decode_json function from the JSON module will always return a data reference.
Suppose you have a Perl program like this
use strict;
use warnings;
use JSON;
my $json_data = '{ "items": [ 1, 2, 3, 4 ] }';
my $content = decode_json($json_data);
use Data::Dump;
dd $content;
which outputs this text
{ items => [1 .. 4] }
showing that $content is a hash reference. Then you can access the array reference, as you found, with
dd $content->{items};
which shows
[1 .. 4]
and you can print the first element of the array by writing
print $content->{items}[0], "\n";
which, again as you have found, shows just
1
which is the first element of the array.
As #cjm mentions in a comment, it is imperative that you use strict and use warnings at the start of every Perl program. If you had those in place in the program where you tried to access $content{items}, your program would have failed to compile, and you would have seen the message
Global symbol "%content" requires explicit package name
which is a (poorly-phrased) way of telling you that there is no %content so there can be no items element.
The scalar variable $content is completely independent from the hash variable %content, which you are trying to access when you write $content{items}. %content has never been mentioned before and it is empty, so there is no items element. If you had tried #{$content->{items}} then it would have worked, as would #{${$content}{items}}
If you really have a problem with the arrow operator, then you could write
print ${$content}{items}[0], "\n";
which produces the same output; but I don't understand what is wrong with the original version.

Decode a JSON file with PHP

I have a classic JSON problem, and i know that many post are asking about that...
But i doubt that the JSON i try to grab has a correct structure.
The files Begin like that :
[{
"time":"0-12h",
"articles":[
{
"id":1,
"domain_id":22,
"title":"Hi Guys"
}
{
"id":2,
"domain_id":17,
"title":"Hi everyone"
}
]
}]
I have try a lot of combinaison to echo the title :
$data = json_decode($json, true);
echo $data->articles;
Or
echo $data->articles->title;
Or
echo $data->articles[0]->title;
Nothing works... :(
Can you help me ?
Thanks !
The second argument true to json_decode() means it should create associative arrays rather than objects for {} in the JSON. So in addition to dealing with the indexed arrays as Explosion Pills points out, you also need to use array syntax to access the keyed elements:
$data[0]['articles'][0]['title']
If you want to be able to use -> syntax, leave out the second argument or set it to false.
I'm hoping the missing comma in the JSON is an error when transcribing to the question. If not, you also need to fix the code that creates the JSON in the first place.
$data itself is an array. Try
$data[0]->articles[0]->title;
Also the JSON is not valid (missing a comma before the second articles array element).
there is a comma , missing
}
,
{
json_decode with the second parameter true returns an array
print_r($data['articles']);
echo $data['articles'] would output Array

merging json objects in php

how can i merge two json objects in php
one of array is like this
$arr_data = array('id'=>$country_id);
$arr = json_encode($arr_data);
and another one is like this:
$arr_places = json_encode($xmlDoc);
now I want to merge them into a single json object. How can I do this.
It depends very much on what you mean by "merge". Just a plain merge or you will need to eliminate the duplicated attributes?...etc.
The simplest way is just like what xdazz mentioned.
So you should merge the array first and then use json_encode.
$json = json_encode(array_merge($arr_data, $xmlDoc));
Merge the results and then encode.
$arr_data = array('id'=>$country_id);
$res = array_merge( $arr_data, $xmlDoc );
$merged = json_encode($res);
Most of the answers here assume that this is a case where one is confronted with two arrays, rather than objects. OP was asking about merging two objects into a single JSON.
While there are many solutions to this, I've got a hack that goes a step further and actually merges the objects into a single JSON string by converting the objects to JSON strings, then back to associative arrays, and then back to JSON.
Could be an efficiency fail, but does the job :-) Here's a code sample:
/**
* Merges two objects into a single JSON structure
* #param object $obj1
* #param object $obj2
* #return string the resuling JSON string
*/
function mergeToJSON($obj1, $obj2) {
$json1 = json_encode($obj1);
$json2 = json_encode($obj2);
if ($json1 === FALSE OR $json2 === FALSE) {
return "";
}
$array1 = json_decode($json1, TRUE);
$array2 = json_decode($json2, TRUE);
$data = array_merge($array1, $array2);
return json_encode($data);
}
The above mentioned solution is not working for me with PHP version 5.5.12
What I want to is in short append to json strings and form one json string out of it, as explained below:
$str1 = {
timestamp: "2015-04-03T08:08:51+00:00",
user: "admin",
src_ip: "127.0.0.1"
}
$str2 = {
timestamp: "2015-04-03T08:08:51+00:00",
user: "Peter_x",
src_ip: "127.0.0.1"
}
$value1 = json_decode ($str1, TRUE);
$value2 = json_decode ($str2, TRUE);
$combined = array_merge ($value1, $value2);
$combined_json = json_encode ($combined);
file_put_contents("c:\outputfile", $combined_json, FILE_APPEND);
The result is:
{
"timestamp": "2015-04-03T08:08:51+00:00",
"user": "admin",
"src_ip": "127.0.0.1",
}
{
"timestamp": "2015-04-03T08:08:51+00:00",
"user": "Peter_x",
"src_ip": "127.0.0.1",
}
Instead I expect one single json string. Firefox fails to parse it. What surprises me is that in the resulting string the keys are within quotes. (e.g: "timestamp").
Can any one tell me what is wrong with the code or how to join the two json strings to one?