Deano
Deano

Reputation: 12230

Append an object to a json file

I'm trying to append

 {"create": {"_index":"socteam", "_type":"products"}}

To this json

{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}

I have tried

{"create": {"_index":"socteam", "_type":"products"}},
{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}

But I keep getting unexpected end of input, I'm trying to define an index for this data before its gets inserted to elastic search.

Upvotes: 0

Views: 166

Answers (2)

Marty
Marty

Reputation: 2808

As the commenters said, you either make a 2-element array, or merge two hashes togeather (resulting in the second clobbering any entries with identical keys);

use v5.12;
use JSON::Tiny qw(decode_json encode_json);

my $j1 = decode_json '{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}' ;

my $j2 = decode_json '{"create": {"_index":"socteam", "_type":"products"}}' ;

my @merged_array = ( %$j1 , %$j2 );
my %merged_hash  = ( %$j1 , %$j2 );

say encode_json \@merged_array ;
say "========================" ;
say encode_json \%merged_hash  ;

which produces;

["general",{"log_path":"STDIN","unique_static_files":0,"generation_time":0,"unique_referrers":0,"unique_not_found":0,"date_time":"2016-04-1806:50:40","failed_requests":0,"bandwidth":1233,"unique_files":11,"excluded_hits":0,"log_size":0,"total_requests":12,"valid_requests":12,"unique_visitors":12},"requests",[{"bytes":1233,"hits":12,"percent":100,"data":"2xxSuccess","items":[{"bytes":1233,"hits":12,"percent":100,"data":"200-OK:Therequestsentbytheclientwassuccessful","visitors":12}],"visitors":12}],"visitors",[{"bytes":1233,"hits":12,"percent":100,"data":"20160418","visitors":12}],"create",{"_index":"socteam","_type":"products"}]
========================
{"create":{"_index":"socteam","_type":"products"},"general":{"log_path":"STDIN","unique_static_files":0,"generation_time":0,"unique_referrers":0,"unique_not_found":0,"date_time":"2016-04-1806:50:40","failed_requests":0,"bandwidth":1233,"unique_files":11,"excluded_hits":0,"log_size":0,"total_requests":12,"valid_requests":12,"unique_visitors":12},"requests":[{"bytes":1233,"hits":12,"percent":100,"data":"2xxSuccess","items":[{"bytes":1233,"hits":12,"percent":100,"data":"200-OK:Therequestsentbytheclientwassuccessful","visitors":12}],"visitors":12}],"visitors":[{"bytes":1233,"hits":12,"percent":100,"data":"20160418","visitors":12}]}

Upvotes: 0

Daniel Martin
Daniel Martin

Reputation: 23588

You're getting the comments you are because I think you didn't state your question correctly.

It appears that you're attempting to use the elasticsearch bulk API; is this correct?

If so, then what that API accepts is multiple JSON documents, separated by newlines. You don't combine the two JSON objects into another JSON file. Instead, you simply have the two objects as two separate JSON documents on different lines:

{"create": {"_index":"socteam", "_type":"products"}}
{"general":{"date_time":"2016-04-1806:50:40","total_requests":12,"valid_requests":12,"failed_requests":0,"generation_time":0,"unique_visitors":12,"unique_files":11,"excluded_hits":0,"unique_referrers":0,"unique_not_found":0,"unique_static_files":0,"log_size":0,"bandwidth":1233,"log_path":"STDIN"},"visitors":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"20160418"}],"requests":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"2xxSuccess","items":[{"hits":12,"visitors":12,"percent":100.00,"bytes":1233,"data":"200-OK:Therequestsentbytheclientwassuccessful"}]}]}

No comma or anything.

Also, when you contact your elastic search machine, you are using the endpoint /_bulk, yes?

Upvotes: 3

Related Questions