Validating array of relative arrays(maps) with Respect/Validation? - php

I'm trying to validate to following json:
{
"variants": [
{
"variant_code": "1",
"price": 12,
"discount": 12,
"height": 1,
"longitude": 1,
"width": 1,
"weight": 1,
"package_height": 1,
"package_longitude": 1,
"package_width": 1,
"package_weight": 11,
"stock": 1
},
{
"variant_code": "2",
"price": 12,
"discount": 12,
"height": 1,
"longitude": 1,
"width": 1,
"weight": 1,
"package_height": 1,
"package_longitude": 1,
"package_width": 1,
"package_weight": 11,
"stock": 1
},
{
"variant_code": "3",
"price": 12,
"discount": 12,
"height": 1,
"longitude": 1,
"width": 1,
"weight": 1,
"package_height": 1,
"package_longitude": 1,
"package_width": 1,
"package_weight": 11,
"stock": 1
}
]
}
I can't find a way to validate it, I've tried to following methods:
$this->validator->validate(
$request, [
"variants" => v::arrayVal()->each(
v::key("variant_code", v::stringVal()->notEmpty()->length(1, 100)),
v::key("stock", v::intVal()->notOptional()),
v::key("price", v::numericVal()->notEmpty()),
v::key("discount", v::numericVal()->notEmpty()),
v::key("weight", v::numericVal()->notEmpty()),
v::key("width", v::numericVal()->notEmpty()),
v::key("height", v::numericVal()->notEmpty()),
v::key("longitude", v::numericVal()->notEmpty()),
v::key("package_weight", v::numericVal()->notEmpty()),
v::key("package_width", v::numericVal()->notEmpty()),
v::key("package_longitude", v::numericVal()->notEmpty()),
v::key("package_height", v::numericVal()->notEmpty())
)
]
);
But it just validates the first key "variant_code" of each relative array.
I also tried this:
$this->validator->validate(
$request, [
"variants" => v::arrayVal()->each(
v::keySet(
v::key("variant_code", v::stringVal()->notEmpty()->length(1, 100)),
v::key("stock", v::intVal()->notOptional()),
v::key("price", v::numericVal()->notEmpty()),
v::key("discount", v::numericVal()->notEmpty()),
v::key("weight", v::numericVal()->notEmpty()),
v::key("width", v::numericVal()->notEmpty()),
v::key("height", v::numericVal()->notEmpty()),
v::key("longitude", v::numericVal()->notEmpty()),
v::key("package_weight", v::numericVal()->notEmpty()),
v::key("package_width", v::numericVal()->notEmpty()),
v::key("package_longitude", v::numericVal()->notEmpty()),
v::key("package_height", v::numericVal()->notEmpty())
)
)
]
);
But it throws the following error:
{
"variants": {
"variants": "Must have keys `{ \"variant_code\", \"stock\", \"price\", \"discount\", ... }`"
}
I've also tryied many other ways unsuccessfully.
I'm working with Respect/Validation version 2.0 and PHP version 7.4.
Anyone knows how to do it, with respect/validation?(I already know how to do it manually). Thank you.

The problem here is the product_image_id key that is required by your rules but it's not present in the data you're trying to validate.
What I tried:
$string = file_get_contents(__DIR__ . "./data.json");
$json = json_decode($string, true);
function validate($data) {
v::arrayVal()->each(
v::keySet(
v::key("variant_code", v::stringVal()->notEmpty()->length(1, 100)),
v::key("product_image_id", v::optional(v::intVal())), // This key is missing in the data.json file
v::key("stock", v::intVal()->notOptional()),
v::key("price", v::numericVal()->notEmpty()),
v::key("discount", v::numericVal()->notEmpty()),
v::key("weight", v::numericVal()->notEmpty()),
v::key("width", v::numericVal()->notEmpty()),
v::key("height", v::numericVal()->notEmpty()),
v::key("longitude", v::numericVal()->notEmpty()),
v::key("package_weight", v::numericVal()->notEmpty()),
v::key("package_width", v::numericVal()->notEmpty()),
v::key("package_longitude", v::numericVal()->notEmpty()),
v::key("package_height", v::numericVal()->notEmpty())
)
)->assert($data);
}
try {
/*
$array = [
[
"variant_code" => "qwerty"
]
];
$result = validate($array);
*/
validate($json["variants"]);
echo ("Validation ok");
} catch(NestedValidationException $exception) {
echo($exception->getFullMessage());
echo ("Validation error");
}
data.json file:
{
"variants": [
{
"variant_code": "1",
"price": 12,
"discount": 12,
"height": 1,
"longitude": 1,
"width": 1,
"weight": 1,
"package_height": 1,
"package_longitude": 1,
"package_width": 1,
"package_weight": 11,
"stock": 1
},
{
"variant_code": "2",
"price": 12,
"discount": 12,
"height": 1,
"longitude": 1,
"width": 1,
"weight": 1,
"package_height": 1,
"package_longitude": 1,
"package_width": 1,
"package_weight": 11,
"stock": 1
},
{
"variant_code": "3",
"price": 12,
"discount": 12,
"height": 1,
"longitude": 1,
"width": 1,
"weight": 1,
"package_height": 1,
"package_longitude": 1,
"package_width": 1,
"package_weight": 11,
"stock": 1
}
]
}
You can check my complete code here.

Related

How to sort based on an array inside of an array in PHP?

Let's consider the following JSON response. It is a list of station objects.
[
{
"id": 43,
"trucks": [
{
"id": 24,
"vehicleType": {
"id": 2,
"vehicleType": "VT",
"createdAt": 1622645476000,
"updatedAt": 1622645476000
},
"sensor": {
"id": 16,
"devId": "tutorial",
"readings": [
{
"id": 36296,
"interiorTemperature": 528.36,
"batteryTemperature": 528.36,
"batteryVoltage": 0,
"createdAt": 1623065279000,
"updatedAt": 1623065279000
}
],
"resolved": false,
"createdAt": 1622645598000,
"updatedAt": 1622650553000
},
"vehicleNumber": 123456,
"truckStatus": true,
"defaultBatteryVoltage": "12",
"batteryTempThreshold": 25,
"interiorTempThresholdMax": 30,
"interiorTempThresholdMin": 5,
"batteryVoltageThreshold": 11.5,
"createdAt": 1622645517000,
"updatedAt": 1622645663000
},
{
"id": 29,
"vehicleType": {
"id": 2,
"vehicleType": "VT",
"createdAt": 1622645476000,
"updatedAt": 1622645476000
},
"sensor": {
"id": 23,
"devId": "value",
"readings": [
{
"id": 36298,
"interiorTemperature": 10,
"batteryTemperature": 10,
"batteryVoltage": 10,
"createdAt": 1623074142000,
"updatedAt": 1623074142000
}
],
"resolved": false,
"createdAt": 1623074089000,
"updatedAt": 1623074089000
},
"vehicleNumber": 313215,
"truckStatus": true,
"defaultBatteryVoltage": "12",
"batteryTempThreshold": 50,
"interiorTempThresholdMax": 50,
"interiorTempThresholdMin": 50,
"batteryVoltageThreshold": 50,
"createdAt": 1623073365000,
"updatedAt": 1623073365000
}
],
"name": "value",
"city": "value",
"address": "value",
"zipCode": "value",
"createdAt": 1622645352000,
"updatedAt": 1622645352000
},
{
"id": 44,
"trucks": [
{
"id": 25,
"vehicleType": {
"id": 3,
"vehicleType": "AG",
"createdAt": 1622647436000,
"updatedAt": 1622647436000
},
"sensor": {
"id": 18,
"devId": "test",
"readings": [
null
],
"resolved": false,
"createdAt": 1622795394000,
"updatedAt": 1622795394000
},
"vehicleNumber": 123457,
"truckStatus": true,
"defaultBatteryVoltage": "24",
"batteryTempThreshold": 80,
"interiorTempThresholdMax": 50,
"interiorTempThresholdMin": -20,
"batteryVoltageThreshold": 11.5,
"createdAt": 1622645584000,
"updatedAt": 1623074846000
}
],
"name": "value",
"city": "value",
"address": "value",
"zipCode": "11112",
"createdAt": 1622645431000,
"updatedAt": 1622645431000
},
{
"id": 46,
"trucks": [
{
"id": 26,
"vehicleType": {
"id": 2,
"vehicleType": "VT",
"createdAt": 1622645476000,
"updatedAt": 1622645476000
},
"sensor": null,
"vehicleNumber": 979787,
"truckStatus": true,
"defaultBatteryVoltage": "12",
"batteryTempThreshold": 123,
"interiorTempThresholdMax": 123,
"interiorTempThresholdMin": 123,
"batteryVoltageThreshold": 123,
"createdAt": 1623064671000,
"updatedAt": 1623064671000
}
],
"name": "value",
"city": "value",
"address": "value",
"zipCode": "11111",
"createdAt": 1622918304000,
"updatedAt": 1622918304000
}
]
All stations have an id, a list of trucks, and some basic info (name, location etc). With PHP, I convert this to an array object. What I would like to achieve is to sort the array. Now I have managed to sort based on name or location. I used the following snipped:
usort($stations, function($a, $b) {
$retval = $a['name'] <=> $b['name'];
return $retval;
});
Now it's all nice but we would like to sort it based on the highest interior temperature read in a station. To make the station with the highest interior temperature be the 0th element in the list and the one with the lowest (or no reading at all) is the nth. I have tried playing around with usort but I had no results. Is it even possible to sort it like that?

Delete key 'data' of collection Laravel

I need to remove the 'data' key from my collection in Laravel.
This worked for me, but it removed the other keys that I cared to keep, I just need to remove the 'data' key:
return $filteredValues ​​= $collection->values ​()->all(); // I remove other keys inside the objects.
My collection return:
$records = Item::where('tienda_id',$id)->where('item.nombre', 'like', "%" . $query . "%")->take(50)->get();
return $collection = new ItemCollection($records);
My ItemCollection.php
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Storage;
class ItemCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* #param \Illuminate\Http\Request $request
* #return mixed
*/
public function toArray($request)
{
return $this->collection->transform(function($row, $key) {
return [
'id' => $row->id,
'nombre' => $row->nombre,
'marca_id' => $row->marca_id,
'tienda_id' => $row->tienda_id,
'nombre_marca' => $row->marca->nombre_marca,
'unidad_id' => $row->unidad_id,
'nombre_unidad' => $row->unidad->nombre_unidad,
'tipo_cambio' => $row->tienda->tipocambio,
'categoria_id' => $row->categoria_id,
'stock' => $row->stock,
'moneda' => $row->moneda,
'codigos' => $row->codigos,
'stockminimo' => $row->stockminimo,
'stockmaximo' => $row->stockmaximo,
'impuesto_id' => $row->impuesto_id,
'primer_margen' => $row->primer_margen,
'segundo_margen' => $row->segundo_margen,
'precio' => $row->precio,
'notas' => $row->notas,
'imagen' => url('images/'.$row->imagen),
];
});
}
}
My JSON:
{
"data": [
{
"id": 27,
"nombre": "Nombre de prueba",
"marca_id": 2,
"tienda_id": 2,
"nombre_marca": "marca 2",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 100,
"moneda": "$",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 2,
"nombre": "66677kj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 5,
"nombre": "78877k"
}
}
],
"stockminimo": 1,
"stockmaximo": 100,
"impuesto_id": 1,
"primer_margen": "35.00",
"segundo_margen": "20.00",
"precio": "5.07",
"notas": "jkjkkjkkj",
"imagen": "http://maks.test/images/159692202782450637_121718839341269_2075616741920079872_o.jpg"
},
{
"id": 28,
"nombre": "Aleta de pollo",
"marca_id": 1,
"tienda_id": 2,
"nombre_marca": "marca1x",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 5,
"moneda": "S/",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 2,
"nombre": "jhjjkj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 5,
"nombre": "jkk"
}
}
],
"stockminimo": 6,
"stockmaximo": 88,
"impuesto_id": 1,
"primer_margen": "2.00",
"segundo_margen": "10.00",
"precio": "100.00",
"notas": "jkjkkjjk",
"imagen": "http://maks.test/images/159698573182341068_123146022531884_1345097685962588160_o.jpg"
}
]
}
I need it to look like this:
[
{
"id": 27,
"nombre": "Nombre de prueba",
"marca_id": 2,
"tienda_id": 2,
"nombre_marca": "marca 2",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 100,
"moneda": "$",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 2,
"nombre": "66677kj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 27,
"codigo_id": 5,
"nombre": "78877k"
}
}
],
"stockminimo": 1,
"stockmaximo": 100,
"impuesto_id": 1,
"primer_margen": "35.00",
"segundo_margen": "20.00",
"precio": "5.07",
"notas": "jkjkkjkkj",
"imagen": "http://maks.test/images/159692202782450637_121718839341269_2075616741920079872_o.jpg"
},
{
"id": 28,
"nombre": "Aleta de pollo",
"marca_id": 1,
"tienda_id": 2,
"nombre_marca": "marca1x",
"unidad_id": 59,
"nombre_unidad": "NIU",
"tipo_cambio": "3.54",
"categoria_id": 1,
"stock": 5,
"moneda": "S/",
"codigos": [
{
"id": 2,
"nombre_codigo": "Codigo",
"tienda_id": 2,
"created_at": "2020-08-03T15:19:11.000000Z",
"updated_at": "2020-08-06T22:38:02.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 2,
"nombre": "jhjjkj"
}
},
{
"id": 5,
"nombre_codigo": "CODIGO 2",
"tienda_id": 2,
"created_at": "2020-08-07T20:45:29.000000Z",
"updated_at": "2020-08-07T20:45:29.000000Z",
"pivot": {
"item_id": 28,
"codigo_id": 5,
"nombre": "jkk"
}
}
],
"stockminimo": 6,
"stockmaximo": 88,
"impuesto_id": 1,
"primer_margen": "2.00",
"segundo_margen": "10.00",
"precio": "100.00",
"notas": "jkjkkjjk",
"imagen": "http://maks.test/images/159698573182341068_123146022531884_1345097685962588160_o.jpg"
}
]
Thanks in advance for reading me, I hope you can help me solve this problem, thanks!
In AppProvider.php or similar add the following. This will disable data wrapping, for ItemCollections. Thou i would want you to reconsider, if you need to use pagination or meta attributes on your responses, you have no where to put em withouth data wrapping and thats one of the reason it is used.
public function boot()
{
ItemCollection::withoutWrapping();
}

How to manually map collection using laravel/php and get total amount

I currently have the below json response that the API is returning. I am able to return it using Laravel Eloquent. There are several users and each user has a several receipts. A receipt has types and status. I want to try to get the total sum amount for each receipt that is related to its type and status. I was able to return the below json response using
$this->user->with('receipts')->has('receipts')->get(['id', 'name']);
I have tried using multiple laravel collections methods https://laravel.com/docs/5.8/collections#available-methods But I am still unable to get the desired response.
[
{
"id": 1,
"name": "kent",
"receipts": [
{
"id": 1,
"user_id": 1,
"type_id": 1,
"status": 0,
"amount": 100
},
{
"id": 2,
"user_id": 1,
"type_id": 1,
"status": 0,
"amount": 100
},
{
"id": 3,
"user_id": 1,
"type_id": 2,
"status": 1,
"amount": 50
},
{
"id": 4,
"user_id": 1,
"type_id": 2,
"status": 0,
"amount": 30
},
{
"id": 5,
"user_id": 1,
"type_id": 2,
"status": 0,
"amount": 30
},
{
"id": 6,
"user_id": 1,
"type_id": 1,
"status": 0,
"amount": 20
},
{
"id": 7,
"user_id": 1,
"type_id": 1,
"status": 1,
"amount": 10
}
]
},
{
"id": 2,
"name": "allison",
"receipts": [
{
"id": 9,
"user_id": 2,
"type_id": 1,
"status": 0,
"amount": 20
}
]
}
]
I expect to get this:
[
{
"id": 1,
"name": "kent",
"receipts": [
{
"performance and deleted": 220,
"performance and not deleted": 10,
"project and deleted": 60,
"project and deleted": 50
}
]
},
{
"id": 2,
"name": "allison",
"receipts": [
{
"performance and deleted": 20,
"performance and not deleted": 0,
"project and deleted": 0,
"project and not deleted": 0
}
]
}
]
My main concern is to use laravel collection methods and easy to read code to get my expected result
In this way, I believe you get your expected result with nice readable code.
I'll assume that you have $users variable which contains a list of users in collection (And probably the receipts are already loaded).
// You need to name the keys as you desire. (I can't figure out the labels by the type_id and status numbers).
$statusPair = collect([
'performance and deleted' => [
'type_id' => 1,
'status' => 0,
],
'something' => [
'type_id' => 1,
'status' => 1,
],
'something 2' => [
'type_id' => 2,
'status' => 0,
],
'something 3' => [
'type_id' => 2,
'status' => 1,
],
]);
$data = $users->map(function ($user) use ($statusPair) {
$receipts = $user->receipts;
$sums = $statusPair->mapWithKeys(function ($pair, $statusLabel) use ($receipts) {
$filtered = $receipts;
foreach ($pair as $key => $value) {
$filtered = $filtered->where($key, $value);
}
$sum = $filtered->sum('amount');
return [
$statusLabel => $sum,
];
});
return [
'id' => $user->id,
'name' => $user->name,
'receipts' => $sums->toArray(),
];
});
i think this will help
$user->receipts->sum('amount');

API Resources laravel filter data

So i am trying to get some data pass to an array and send to json.
But when i do this i am getting alot of information:
This is the response:
{
"current_page": 1,
"data": [{
"id": 2,
"object_id": 3,
"booking_id": 1,
"sender_id": 1,
"receiver_id": 2,
"message": "It is accepted",
"type_id": 5,
"read": 1,
"archive": 0,
"star": 0,
"created_at": "2019-02-26 11:45:28",
"updated_at": "2019-02-26 12:15:11",
"created_time": "26\/02\/2019",
"host_user": 0,
"guest_user": 1,
"sender": {
"id": 1,
"first_name": "Joe",
"last_name": "Cos",
"email": "email-91#hotmail.com",
"profile_image": null,
"balance": 0,
"status": "Active",
"created_at": "2019-02-21 15:19:26",
"updated_at": "2019-02-21 15:19:26",
"profile_src": "http:\/\/xxx.com\/public\/images\/user_pic-225x225.png"
},
"bookings": {
"id": 1,
"object_id": 3,
"code": "mYuL4p",
"host_id": 1,
"user_id": 2,
"start_date": "2019-02-26",
"end_date": "2019-02-27",
"status": "Accepted",
"guest": 0,
"total_night": 1,
"per_night": 20,
"base_price": 20,
"cleaning_charge": 0,
"guest_charge": 0,
"service_charge": 0,
"security_money": 0,
"host_fee": 0,
"total": 20,
"booking_type": "request",
"currency_code": "EUR",
"cancellation": "Flexible",
"transaction_id": "67427302T32774838",
"payment_method_id": 1,
"accepted_at": "2019-02-26 11:45:28",
"expired_at": null,
"declined_at": null,
"cancelled_at": null,
"cancelled_by": null,
"created_at": "2019-02-26 11:37:36",
"updated_at": "2019-02-26 11:45:28",
"host_payout": 23,
"label_color": "success",
"date_range": "Feb 26 - 27, 2019",
"expiration_time": "2019\/02\/27 11:37:36",
"currency": {
"id": 3,
"name": "Europe",
"code": "EUR",
"symbol": "€",
"rate": "0.88",
"status": "Active",
"default": "0",
"org_symbol": "€"
}
},
"object_address": {
"id": 3,
"object_id": 3,
"address_line_1": "XXXXXXXXX, 4050-352 Porto, Portugal",
"address_line_2": null,
"latitude": "49.999",
"longitude": "-8.88810419921",
"city": "P",
"state": "P",
"country": "P",
"postal_code": "4050-352"
}
}],
"from": 1,
"last_page": 1,
"next_page_url": null,
"per_page": 10,
"prev_page_url": null,
"to": 1,
"total": 1
}
I am using the API Resources
In MY Resource i have:
return parent::toArray($request);
With this i get all that information my problem is i need to filter some data
and when i try to filter for exemple the email.
return [
'email'=> $this->email,
];
it gives error and says
"Property [email] does not exist on this collection instance."
I am learning and i am a newbie i am sorry for this dumb questions.
Thanks to all!
First make a trait
trait ResourceHelpers
{
/**
* Remove null values from Eloquent api resource
* #param array $data
* #return array
*/
public function removeNullValues(array $data)
{
$filtered_data = [];
foreach ($data as $key => $value) {
$filtered_data[$key] = $this->when($value !== null, $value);
}
return $filtered_data;
}
}
Then use it in your resource
class UserResource extends JsonResource
{
use ResourceHelpers;
/**
* Transform the resource into an array.
*
* #param \Illuminate\Http\Request $request
* #return array
*/
public function toArray($request)
{
return $this->removeNullValues([
"id" => $this->id,
"first_name" => $this->first_name,
"last_name" => $this->last_name,
"phone" => $this->phone,
"email" => $this->email,
"balance" => $this->balance,
'address' => $this->address,
'city' => $this->city,
'state' => $this->state,
'zip_code' => $this->zip_code,
'country' => CountryResource::make($this->whenLoaded('country')),
"joined_at" => $this->created_at,
"updated_at" => $this->updated_at,
]);
}
}
In your resource:
$return = parent::toArray($request);
unset($return['email']);
return $return;
Or, you can explicitly define just what you do want to return, instead of relying on toArray:
return [
'id' => $this->id,
'message' => $this->message,
// and so on...
];
This gives you more control over the final API response, especially if you make database/model changes but need the API to remain unchanged.

json decode error on some requests but not others with Algolia php library

I'm seeing the following error:
My params are
array (size=4)
'facets' => string 'Instock.1' (length=9)
'facetFilters' => string 'inStock.1:1' (length=11)
'numericFilters' => string 'itemId!=511283' (length=14)
'getRankingInfo' => int 1
If I remove either getRankingInfo or numericFilters, it works for all requests.
According to the logs in Algolia it's returning results, but i don't get that far on my end for some requests.
One response that causes the error to appear is:
Request
{
"params": "facets=Instock.1&facetFilters=inStock.1%3A1&numericFilters=itemId%21%3D511283&getRankingInfo=1&query=WARBURTON+ORANGE+WRAPPER+800G"
}
Response
{
"hits": [
{
"itemId": 506241,
"name": "Warburton Blue Wrapper 800g",
"slug": "506241-warburton-blue-wrapper",
"brand": "Warburton",
"supplier_name": "Warburtons Bread",
"size": "800G",
"status": "L",
"vat_rate": "0.00",
"popularity": 18992,
"image": "/images/products/506241.jpg",
"thumbnail": "/images/products/506241_thumbnail.jpg",
"barcodes": [
"5010044000039"
],
"branches": [
1,
2,
4,
5,
6
],
"deepestCategory": "07013033337",
"inStock": {
"1": 1,
"2": 0,
"4": 1,
"5": 1,
"6": 1
},
"alternatives": [
],
"objectID": "506241",
"_highlightResult": {
"name": {
"value": "<em>Warburton</em> Blue <em>Wrapper</em> <em>800g</em>",
"matchLevel": "partial",
"matchedWords": [
"warburton",
"wrapper",
"800g"
]
},
"barcodes": [
{
"value": "5010044000039",
"matchLevel": "none",
"matchedWords": [
]
}
]
},
"_rankingInfo": {
"nbTypos": 0,
"firstMatchedWord": 0,
"proximityDistance": 10,
"userScore": 21542,
"geoDistance": 0,
"geoPrecision": 1,
"nbExactWords": 3,
"words": 3
}
},
{
"itemId": 511294,
"name": "Warburton Green Wrapper ",
"slug": "511294-warburton-green-wrapper",
"brand": "Warburton",
"supplier_name": "Warburtons Bread",
"size": "",
"status": "L",
"vat_rate": "0.00",
"popularity": 78098,
"image": "/images/products/511294.jpg",
"thumbnail": "/images/products/511294_thumbnail.jpg",
"barcodes": [
"5010044002347"
],
"branches": [
1,
2,
4,
5,
6
],
"deepestCategory": "07013033337",
"inStock": {
"1":
One that works fine is:
Request body
{
"params": "facets=Instock.1&facetFilters=inStock.1%3A1&numericFilters=itemId%21%3D558471&getRankingInfo=1&query=I+CAN%27T+BELIEVE+IT%27S+NOT+BUTTER+2KG"
}
Response
{
"hits": [
{
"itemId": 581691,
"name": "I Can't Believe Its Not Butter 500g",
"slug": "581691-i-cant-believe-its-not-butter",
"brand": "I Can't Believe",
"supplier_name": "U.lever Bestfoods (chilled)",
"size": "500G",
"status": "L",
"vat_rate": "0.00",
"popularity": 36597,
"image": "/images/products/581691.jpg",
"thumbnail": "/images/products/581691_thumbnail.jpg",
"barcodes": [
"000118039904",
"05000118039904",
"5000241007009"
],
"branches": [
1,
2,
3,
4,
5,
6
],
"deepestCategory": "07015269103",
"inStock": {
"1": 1,
"2": 1,
"3": 1,
"4": 1,
"5": 1,
"6": 1
},
"alternatives": [
],
"objectID": "581691",
"_highlightResult": {
"name": {
"value": "<em>I</em> <em>Can't</em> <em>Believe</em> <em>Its</em> <em>Not</em> <em>Butter</em> 500g",
"matchLevel": "partial",
"matchedWords": [
"i",
"can",
"t",
"believe",
"it",
"s",
"not",
"butter"
]
},
"barcodes": [
{
"value": "000118039904",
"matchLevel": "none",
"matchedWords": [
]
},
{
"value": "05000118039904",
"matchLevel": "none",
"matchedWords": [
]
},
{
"value": "5000241007009",
"matchLevel": "none",
"matchedWords": [
]
}
]
},
"_rankingInfo": {
"nbTypos": 0,
"firstMatchedWord": 0,
"proximityDistance": 22,
"userScore": 15914,
"geoDistance": 0,
"geoPrecision": 1,
"nbExactWords": 6,
"words": 8
}
},
{
"itemId": 247577,
"name": "I Can't Believe Its Not Butter 250g"
It seems the result set was causing an error with json_decode. When I limited the hitsPerPage to 10 all works fine. So the answer here is to paginate large sets, obvious really.
Thanks to all who read and commented.

Categories