How access array inside of JSON? - json

I'm use Guzzle for Laravel to send POST for Webservice, but I can't access array to send picture. That is the structed to I need follow:
{
"title": "xxxxxxxxxxxxxxxxx",
"category_id": "XXXX",
"official_store_id": null,
"pictures": [
{
"id": "XXXXX",
"url": "http://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"secure_url": "https://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"size": "500x500",
"max_size": "500x500",
"quality": ""
}
],
}
I try to send it like this:
$r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token='.$token->erp_access_token, [
'json' => [
'title' => $name,
'category_id' => $cat2,
'price' => $price,
'currency_id' => 'BRL',
'available_quantity' => $quantity,
'buying_mode' => 'buy_it_now',
'listing_type_id' => 'gold_special',
'condition' => 'new',
'description' => $description,
'pictures' => [
'url' => $image
]
]
]);
Returns the error:
{"message":"body.invalid_field_types","error":"[invalid property type: [pictures] expected List but was JSONObject value (truncated...)
I read the Guzzle docs, but I not found any example for that case.
Any suggestion?

Change your pictures value from this
'pictures' => [
'url' => $image
]
to this
'pictures' => [
['url' => $image]
]

Related

Groovy: convert url encoded to JSON

I have a string from encoded url like this:
leads%5Bupdate%5D%5B0%5D%5Bid%5D=123432456&leads%5Bupdate%5D%5B0%5D%5Bname%5D=%D0%A1%D0%B4%D0%B5%D0%BB%D0%BA%D0%B0+2342345345345&leads%5Bupdate%5D%5B0%5D%5Bstatus_id%5D=45645676&leads%5Bupdate%5D%5B0%5D%5Bprice%5D=0&leads%5Bupdate%5D%5B0%5D%5Bresponsible_user_id%5D=2423432&leads%5Bupdate%5D%5B0%5D%5Blast_modified%5D=1231423556&leads%5Bupdate%5D%5B0%5D%5Bmodified_user_id%5D=6756756&leads%5Bupdate%5D%5B0%5D%5Bcreated_user_id%5D=2423432&leads%5Bupdate%5D%5B0%5D%5Bdate_create%5D=1234534678&leads%5Bupdate%5D%5B0%5D%5Bpipeline_id%5D=4564564&leads%5Bupdate%5D%5B0%5D%5Baccount_id%5D=34578987&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bid%5D=567834&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bname%5D=MANGO+OFFICE+%D0%9D%D0%BE%D0%BC%D0%B5%D1%80+%D0%BB%D0%B8%D0%BD%D0%B8%D0%B8&leads%5Bupdate%5D%5B0%5D%5Bcustom_fields%5D%5B0%5D%5Bvalues%5D%5B0%5D%5Bvalue%5D=74994567456&leads%5Bupdate%5D%5B0%5D%5Bcreated_at%5D=1234534678&leads%5Bupdate%5D%5B0%5D%5Bupdated_at%5D=1231423556&account%5Bsubdomain%5D=google&account%5Bid%5D=34578987&account%5B_links%5D%5Bself%5D=https%3A%2F%2Fgoogle.site.com
decoded string:
leads[update][0][id]=123432456&leads[update][0][name]=Сделка+2342345345345&leads[update][0][status_id]=45645676&leads[update][0][price]=0&leads[update][0][responsible_user_id]=2423432&leads[update][0][last_modified]=1231423556&leads[update][0][modified_user_id]=6756756&leads[update][0][created_user_id]=2423432&leads[update][0][date_create]=1234534678&leads[update][0][pipeline_id]=4564564&leads[update][0][account_id]=34578987&leads[update][0][custom_fields][0][id]=567834&leads[update][0][custom_fields][0][name]=MANGO+OFFICE+Номер+линии&leads[update][0][custom_fields][0][values][0][value]=74994567456&leads[update][0][created_at]=1234534678&leads[update][0][updated_at]=1231423556&account[subdomain]=google&account[id]=34578987&account[_links][self]=https://google.site.com
array:
[
'leads' => [
'update' => [
[
'id' => 22652446,
'name' => 'Сделка 74956462949',
'status_id' => 33687064,
'price' => 0,
'responsible_user_id' => 5827756,
'last_modified' => 1675346949,
'modified_user_id' => 8775297,
'created_user_id' => 5827756,
'date_create' => 1640068981,
'pipeline_id' => 3365869,
'account_id' => 28791871,
'custom_fields' => [
[
'id' => 662156,
'name' => 'MANGO OFFICE Номер линии',
'values' => [
[
'value' => 74994551765,
],
],
],
],
'created_at' => 1640068981,
'updated_at' => 1675346949,
],
],
],
'account' => [
'subdomain' => 'google',
'id' => 28791871,
'_links' => [
'self' => 'https://google.site.com'
],
],
]
and JSON:
{
"leads":
{
"update": [
{
"id":22652446,
"name":"\u0421\u0434\u0435\u043b\u043a\u0430 74956462949",
"status_id":33687064,
"price":0,
"responsible_user_id":5827756,
"last_modified":1675346949,
"modified_user_id":8775297,
"created_user_id":5827756,
"date_create":1640068981,
"pipeline_id":3365869,
"account_id":28791871,
"custom_fields": [
{
"id":662156,
"name":"MANGO OFFICE \u041d\u043e\u043c\u0435\u0440 \u043b\u0438\u043d\u0438\u0438",
"values": [
{
"value":74994551765
}
]
}
],
"created_at":1640068981,
"updated_at":1675346949
}
]
},
"account":
{
"subdomain":"google",
"id":28791871,
"_links":
{
"self":"https:\/\/google.site.com"
}
}
}
How I can convert it to JSON with Groovy?
Thanks.
P.S: I read this Convert url encoded data to json but it's not help.

add to JSON ($payload={}) on condition

I feed JSON to some webhook to trigger a notification (M$ Teams). This works well. However, I want to extend my Perl script: I need to add a new node to my "messagecard" construct on a certain condition.
E.g. I defined this:
my $payload={};
$payload = {
'#type' => 'MessageCard',
'#context' => 'http://schema.org/extensions',
themeColor => $event{COLOR},
text => $event{SERVICEOUTPUT},
sections => [{
facts => [{
name => 'Type',
value => "$event{NOTIFICATIONTYPE} $event{ADDITIONALINFO}"
},
]
}],
potentialAction => [{
'#type' => "OpenUri",
name => "View Monitoring",
targets => [{
os => "default",
uri => $naemon_url
}]
}]
};
$ua = LWP::UserAgent->new;
my $req = POST($opt_webhook
, 'Content-Type' => 'application/json; charset=UTF-8'
, 'Content' => encode_json($payload)
);
my $resp = $ua->request($req);
And if (conditon), I want to extend this as follows (order is important):
$payload = {
'#type' => 'MessageCard',
'#context' => 'http://schema.org/extensions',
themeColor => $event{COLOR},
text => $event{SERVICEOUTPUT},
sections => [{
facts => [{
name => 'Type',
value => "$event{NOTIFICATIONTYPE} $event{ADDITIONALINFO}"
},
]
}],
potentialAction => [{
'#type' => "OpenUri",
name => "View Monitoring",
targets => [{
os => "default",
uri => $naemon_url
}]
},
{
'#type' => "OpenUri",
name => "Notes (Logs, Docs,..)",
targets => [{
os => "default",
uri => $event{SERVICENOTESURL}
}]
}]
};
I am unsure how this can be achieved. Can anyone please provide wisdom how to tackle this?
You can push into the array reference that you've got inside your potentialAction key. In order to do that, you need to dereference it as an array.
my $payload = {
'#type' => 'MessageCard',
potentialAction => [{
name => "View Monitoring",
targets => [{
os => "default",
}]
}]
};
if ($maybe) {
push #{ $payload->{potentialAction} }, {
name => "Notes (Logs, Docs,..)",
targets => [{
os => "default",
}]
};
}
If your Perl version is 5.24 or newer you can also use postfix dereferencing, which some people find easier to read.
push $payload->{potentialAction}->#*, ...
See perlref and perlreftut for more information.

How to also get custom fields of documents from Elasticsearch using Laravel Scout Elasticsearch Driver?

Below is the link to the Scout Elasticsearch Drive which I am using in my Laravel project.
https://github.com/babenkoivan/scout-elasticsearch-driver
I have database table users with the following columns:
id Integer
name String
created_at DateTime
Following the documentation on GitHub, my mapping in the User model looks like followings:
use Searchable; // using Searchable trait.
protected $mapping = [
'properties' => [
'name' => [
'type' => 'text',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
'created_at' => [
'type' => 'date',
'format' => 'dd-MM-yyyy HH:mm',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
// Custom field...
'created_date' => [
'type' => 'date',
'format' => 'dd-MM-yyyy',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
// Custom field...
'created_time' => [
'type' => 'date',
'format' => 'HH:mm',
'fields' => [
'raw' => [
'type' => 'keyword',
],
],
],
]
]
Below is the implementation of the toSearchableArray() function.
public function toSearchableArray()
{
$array = $this->toArray();
// Pre-format the custom fields before inserting them into Elasticsearch.
$array['created_date'] = $this->created_at->format('d-m-Y');
$array['created_time'] = $this->created_at->format('h:i');
return $array;
}
When using curl -X GET command I get the following results and of course with the custom fields.
"hits" : [
{
"_source" : {
"id" : 3,
"name": "John Doe",
"created_at": "31-12-2018 23:59",
"created_date": "31-12-2018", // My custom field.
"created_time": "23:59" // My custom field.
}
]
In my index() action in my controller, I query the data using the following code.
public function index()
{
return User::search("*")->get();
}
I get the records with only the original attributes, matching those columns in the database table as following.
[
{
"id" : 3,
"name": "John Doe",
"created_at": "31-12-2018 23:59",
}
]
Note that this is a JSON response provided by default by Laravel, responding to API calls. My custom attributes created_date and created_time do exist in Elasticsearch as well. How can I get them too as result? The reason I created these custom fields is to format the date and time on the server side beforehands so that my client-side does not need to worry about formating the date and time in the for-loop.
When I use User::search("*")->explain(); I do get my custom fields as well in the hits.hits._source{} object.
This should give you results directly from Elastic Search without matching up with your related models. I think thats the reason why your custom fields get lost.
public function index()
{
return User::search("*")->raw();
}
Documentation

How to fix "_geoip_lookup_failure" tag with logstash filter "geoip" when posting a json with http to logstash

I am posting a json from an application to logstash wanting to get the location of an IP-adress with logstashes geoip plugin. However i get a _geoip_lookup_failure.
this is my logstash config
http {
port => "4200"
codec => json
}
}
filter{
geoip {
source => "clientip"
}
}
output {
elasticsearch { hosts => ["localhost:9200"] }
stdout { codec => rubydebug }
}
this is what I post to the port :
{'used_credentials': [
{'username': 'l', 'number_of_usages': 1, 'used_commands': {},
'get_access': 'false',
'timestamps': {'1': '04/15/2019, 21:08:54'}, 'password': 'l'}],
'clientip': '192.168.xxx.xx',
'unsuccessfull_logins': 1}
and this is what i get in logstash:
{
"unsuccessfull_logins" => 1,
"#version" => "1",
"used_credentials" => [
[0] {
"username" => "l",
"used_commands" => {},
"password" => "l",
"timestamps" => {
"1" => "04/15/2019, 21:08:54"
},
"number_of_usages" => 1,
"get_access" => "false"
}
],
"clientip" => "192.168.xxx.xx",
"#timestamp" => 2019-04-15T19:08:57.147Z,
"host" => "127.0.0.1",
"headers" => {
"request_path" => "/telnet",
"connection" => "keep-alive",
"accept_encoding" => "gzip, deflate",
"http_version" => "HTTP/1.1",
"content_length" => "227",
"http_user_agent" => "python-requests/2.21.0",
"request_method" => "POST",
"http_accept" => "*/*",
"content_type" => "application/json",
"http_host" => "127.0.0.1:4200"
},
"geoip" => {},
"tags" => [
[0] "_geoip_lookup_failure"
]
}
I don't understand why the input is recognized corectly but goeip does not find it
The problem is that your clientip is in the 192.168.0.0/16 network, which is a private network reserved for local use only, it is not present on the database used by the geoip filter.
The geoip filter will only work with public IP addresses.

How to send JSON request with guzzle6 and Laravel

From an API I need to send the request in this format:
Sample Request Body
{
"first_name": "adam.west",
"last_name": "adam.west",
"email_address": "adam.west#example.com",
"phone": {
"number": "11111111111",
"country_code": "UK",
"phone_type": "Mobile",
"extension": "124"
},
"comment": "comment",
}
so with Laravel and Guzzle6, I write this:
$res1 = $client1->post('https://example.com/someurl', [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer '.$token,
],
'json' => [
'first_name' => $FirstName,
'last_name' => $Surname,
'email_address' => $Email,
'phone' => [
$phone['number'] = $Mobile,
$phone['country_code'] = $MobileCountryCode,
$phone['phone_type'] = 'Mobile',
$phone['extension'] = $request->dialCode,
],
'comment' => $comment,
]
]);
Everything is fine except phone element - What is the proper way to send phone element - because from API response I got error message "Phone could not be empty"