PHP code example of wikimedia / zest-jq

1. Go to this page and download the library: Download wikimedia/zest-jq library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wikimedia / zest-jq example snippets


use Wikimedia\ZestJQ\JQ;

// evalString() returns a Generator that yields each output value.
foreach ( JQ::evalString( '{"name":"jq","version":2}', '.name' ) as $val ) {
    echo $val; // "jq"
}

use Wikimedia\ZestJQ\JQ;
use Wikimedia\ZestJQ\JQUtils;

// Use JQUtils::jsonDecode() to ensure objects are stdClass (not
// arrays), and that all arrays are lists.
$input = JQUtils::jsonDecode( '{"items":[1,2,3]}' );

foreach ( JQ::eval( $input, '.items[]' ) as $val ) {
    echo $val, "\n"; // 1, 2, 3
}

use Wikimedia\ZestJQ\JQ;

$filter = JQ::compile( '.[] | select(. > 2)' );

foreach ( $filter( [1, 2, 3, 4] ) as $val ) {
    echo $val, "\n"; // 3, 4
}
foreach ( $filter( [5, 1, 6] ) as $val ) {
    echo $val, "\n"; // 5, 6
}

use Wikimedia\ZestJQ\JQ;
use Wikimedia\ZestJQ\JQError;

try {
    foreach ( JQ::evalString( '"hello"', '.foo' ) as $val ) {
        // ...
    }
} catch ( JQError $e ) {
    echo $e->getMessage();
}

use Wikimedia\ZestJQ\JQEnv;
use Wikimedia\ZestJQ\JQ;

$env = JQEnv::getStdEnv()->extendEnv( 'def double: . * 2;' );

foreach ( JQ::eval( 5, 'double', null, $env ) as $val ) {
    echo $val; // 10
}

/* PHP */
use Wikimedia\ZestJQ\JQ;
use Wikimedia\ZestJQ\JQUtils;
$store = JQUtils::jsonDecode( $json );

/* PHP */
foreach ( JQ::eval( $store, '.store.book[].author' ) as $val ) {
    var_export( $val ); echo "\n";
}
// → 'Nigel Rees'
// → 'Evelyn Waugh'
// → 'Herman Melville'

/* PHP */
var_export( JQ::eval( $store, '.store.book[0].title' )->current() );
// → 'Sayings of the Century'

/* PHP */
foreach ( JQ::eval( $store, '.. | .price? // empty' ) as $val ) {
    var_export( $val ); echo "\n";
}
// → 8.95
// → 12.99
// → 8.99
// → 19.95

/* PHP */
foreach ( JQ::eval( $store, '.store.book[] | select(.price < 10)' ) as $val ) {
    var_export( $val ); echo "\n";
}
// → (object) array(
// →    'category' => 'reference',
// →    'author' => 'Nigel Rees',
// →    'title' => 'Sayings of the Century',
// →    'price' => 8.95,
// → )
// → (object) array(
// →    'category' => 'fiction',
// →    'author' => 'Herman Melville',
// →    'title' => 'Moby Dick',
// →    'isbn' => '0-553-21311-3',
// →    'price' => 8.99,
// → )

/* PHP */
foreach ( JQ::eval( $store, '.store.book[] | select(has("isbn"))' ) as $val ) {
    var_export( $val ); echo "\n";
}
// → (object) array(
// →    'category' => 'fiction',
// →    'author' => 'Herman Melville',
// →    'title' => 'Moby Dick',
// →    'isbn' => '0-553-21311-3',
// →    'price' => 8.99,
// → )

/* PHP */
var_export( JQ::eval( $store, '.store.book[:2]' )->current() );
// → array (
// →   0 =>
// →   (object) array(
// →      'category' => 'reference',
// →      'author' => 'Nigel Rees',
// →      'title' => 'Sayings of the Century',
// →      'price' => 8.95,
// →   ),
// →   1 =>
// →   (object) array(
// →      'category' => 'fiction',
// →      'author' => 'Evelyn Waugh',
// →      'title' => 'Sword of Honour',
// →      'price' => 12.99,
// →   ),
// → )

/* PHP */
foreach ( JQ::eval( $store, '.store.book[0,-1]' ) as $val ) {
    var_export( $val ); echo "\n";
}
// → (object) array(
// →    'category' => 'reference',
// →    'author' => 'Nigel Rees',
// →    'title' => 'Sayings of the Century',
// →    'price' => 8.95,
// → )
// → (object) array(
// →    'category' => 'fiction',
// →    'author' => 'Herman Melville',
// →    'title' => 'Moby Dick',
// →    'isbn' => '0-553-21311-3',
// →    'price' => 8.99,
// → )
bash
# PHPUnit only
vendor/bin/phpunit

# Single test file
vendor/bin/phpunit tests/phpunit/JQCompileTest.php

# Fix code style
composer fix
bash
# Field access
echo '{"name":"world"}' | zestjq '.name'
# → "world"

# Arithmetic with null input
zestjq -n '1 + 1'
# → 2

# Raw string output
echo '"hello"' | zestjq -r '.'
# → hello

# Compact output
echo '[1,2,3]' | zestjq -c 'map(. * 2)'
# → [2,4,6]

# Multiple outputs
echo 'null' | zestjq -n '1, 2, 3'
# → 1
# → 2
# → 3
bash
echo "$STORE" | zestjq -c '[.. | .price? // empty]'
# → [8.95,12.99,8.99,19.95]