JsonPath
extends ProcessorEntity
in package
Class JsonPath
Processor class to fetch/set values in JSON or an array
Table of Contents
- $id : mixed
- Processor ID.
- $db : ADOConnection|null
- DB connections.
- $details : array<string|int, mixed>
- {@inheritDoc}
- $logger : MonologWrapper
- Logger object.
- $meta : array<string|int, mixed>
- Metadata for the processor.
- $request : Request
- All the request details.
- __construct() : mixed
- details() : array<string|int, mixed>
- Return details for processor.
- process() : DataContainer
- {@inheritDoc}
- val() : mixed|DataContainer
- Process a variable into a final result for the processor.
- generateParams() : array<string|int, mixed>
- Generate the params array for the sql search.
- isDataContainer() : bool
- Validate if a set of data is wrapped in a DataContainer object.
- validateAllowedTypes() : void
- Validate an input for allowed variable types
- validateAllowedValues() : void
- Validate an input for allowed values.
Properties
$id
Processor ID.
public
mixed
$id
= ''
Processor ID.
$db
DB connections.
protected
ADOConnection|null
$db
$details
{@inheritDoc}
protected
array<string|int, mixed>
$details
= ['name' => 'Json Path', 'machineName' => 'json_path', 'description' => <<<DESCRIPTION
Get, set, add or remove values in a JSON string or an array (decoded JSON string).
Examples:
Source JSON:
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"available": false
}
],
"bicycle": {
"color": "red",
"price": 19.95,
"available": true
}
},
"authors": [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
}
Get operation:
processor: json_path
id: get_example
data: json or array input
operation: get
stripslashes: true
escape_wrapping_quotes: true
expression: "$.store.bicycle.price"
Result:
15.95
Set operation:
processor: json_path
id: set_example
data: json or array input
operation: set
expression: "$.store.bicycle.price"
value: 100000000.95
Result:
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"available": false
}
],
"bicycle": {
"color": "red",
"price": 100000000.95,
"available": true
}
},
"authors": [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
}
Add operation:
processor: json_path
id: add_example
data: json or array input
operation: add
expression: "$.store.book"
value: "{'category':'drama','author': 'Jane Austen','title': 'Killing Heidi','price': 0.99,'available': true}"
Result:
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99,
"available": false
}, {
'category': 'drama',
'author': 'Jane Austen',
'title': 'Killing Heidi',
'price': 0.99,
'available': true
}
],
"bicycle": {
"color": "red",
"price": 100000000.95,
"available": true
}
},
"authors": [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
}
Remove operation:
processor: json_path
id: remove_example
data: json or array input
operation: remove
expression: "$.store.book"
field_name: title
Result:
{
"store": {
"book": [
{
"category": "fiction",
"author": "Evelyn Waugh",
"price": 12.99,
"available": false
}
],
"bicycle": {
"color": "red",
"price": 19.95,
"available": true
}
},
"authors": [
"Nigel Rees",
"Evelyn Waugh",
"Herman Melville",
"J. R. R. Tolkien"
]
}
DESCRIPTION
, 'menu' => 'Data operation', 'input' => ['data' => ['description' => 'The input JSON or array.', 'cardinality' => [1, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => ['json', 'array'], 'limitValues' => [], 'default' => null], 'expression' => ['description' => 'The JSON path expression.', 'cardinality' => [1, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => ['text'], 'limitValues' => [], 'default' => '$.*'], 'operation' => ['description' => 'The operation ("get", "set", "add" or "remove").', 'cardinality' => [1, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => ['text'], 'limitValues' => ['get', 'set', 'add', 'remove'], 'default' => 'get'], 'remove_wrapping_quotes' => [
// phpcs:ignore
'description' => 'By default, JSON will return strings wrapped in quotes if the result is a literal string (i.e. N/V pair value). This will result in a data_type of JSON wrapped in double quotes and you will be unable to convert to a literal string or number datatype',
'cardinality' => [0, 1],
'literalAllowed' => true,
'limitProcessors' => [],
'limitTypes' => ['boolean'],
'limitValues' => [],
'default' => false,
], 'stripslashes' => [
// phpcs:ignore
'description' => 'By default, JSON will escape special characters with a backslash. Setting this to true will allow you to remove the escaping',
'cardinality' => [0, 1],
'literalAllowed' => true,
'limitProcessors' => [],
'limitTypes' => ['boolean'],
'limitValues' => [],
'default' => false,
], 'field_name' => [
// phpcs:ignore
'description' => 'The data name/key to insert or delete (used for "add" "remove" operations). If empty and the operation is "add", then is similar to append to array.',
'cardinality' => [0, 1],
'literalAllowed' => true,
'limitProcessors' => [],
'limitTypes' => [],
'limitValues' => [],
'default' => null,
], 'value' => ['description' => 'The data value to insert (used for "set" or "add" operations).', 'cardinality' => [0, 1], 'literalAllowed' => true, 'limitProcessors' => [], 'limitTypes' => [], 'limitValues' => [], 'default' => null]]]
Details of the processor.
$logger
Logger object.
protected
MonologWrapper
$logger
$meta
Metadata for the processor.
protected
array<string|int, mixed>
$meta
$request
All the request details.
protected
Request
$request
Request.
Methods
__construct()
public
__construct(array<string|int, mixed> &$meta, Request &$request[, MonologWrapper|null $logger = null ]) : mixed
Parameters
- $meta : array<string|int, mixed>
-
Metadata for the processor.
- $request : Request
-
The full request object.
- $logger : MonologWrapper|null = null
-
The logger.
Return values
mixed —details()
Return details for processor.
public
details() : array<string|int, mixed>
Return values
array<string|int, mixed> —process()
{@inheritDoc}
public
process() : DataContainer
Tags
Return values
DataContainer —Result of the processor.
val()
Process a variable into a final result for the processor.
public
val(string $key[, bool|null $rawData = false ]) : mixed|DataContainer
This method can be used to process a value in its meta to return a final result that it can use. If the object is a processor, then it will process that down to a final return value, or if the obj is a simple value, then it will return that. Anything else will return an error object.
Setting $realValue to true will force the value to be the actual value, rather than a potential dataContainer.
Parameters
- $key : string
-
The key for the input variable in the meta.
- $rawData : bool|null = false
-
Return the raw data or a DataContainer.
Tags
Return values
mixed|DataContainer —generateParams()
Generate the params array for the sql search.
protected
generateParams(string|null $keyword, array<string|int, mixed>|null $keywordCols, string|null $orderBy, string|null $direction) : array<string|int, mixed>
Parameters
- $keyword : string|null
-
Search keyword.
- $keywordCols : array<string|int, mixed>|null
-
Columns to search for the keyword.
- $orderBy : string|null
-
Order by column.
- $direction : string|null
-
Order direction.
Return values
array<string|int, mixed> —isDataContainer()
Validate if a set of data is wrapped in a DataContainer object.
protected
isDataContainer(mixed $data) : bool
Parameters
- $data : mixed
-
DataContainer or raw data.
Return values
bool —validateAllowedTypes()
Validate an input for allowed variable types
private
validateAllowedTypes(string $type, array<string|int, mixed> $limitTypes, int $min, string $key) : void
Parameters
- $type : string
-
Input value type.
- $limitTypes : array<string|int, mixed>
-
List of limit on variable types.
- $min : int
-
Minimum number of values.
- $key : string
-
The key of the input being validated.
Tags
Return values
void —validateAllowedValues()
Validate an input for allowed values.
private
validateAllowedValues(mixed $val, array<string|int, mixed> $limitValues, int $min, string $key) : void
Parameters
- $val : mixed
-
Input value.
- $limitValues : array<string|int, mixed>
-
List of allowed values.
- $min : int
-
Minimum number of values.
- $key : string
-
The key of the input being validated.