0

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.

2
  • 2
    You need to decide upon a valid structure - do you want an array containing two objects, or to add a create key to the existing object, or what? Commented Apr 18, 2016 at 11:22
  • 5
    A JSON file can only contain one data structure. You currently have two. That is not valid JSON. Commented Apr 18, 2016 at 11:23

2 Answers 2

3

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?

Sign up to request clarification or add additional context in comments.

Comments

0

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}]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.