nutools/lib/ulib/support/php/json_get.php

39 lines
858 B
PHP
Raw Normal View History

#!/usr/bin/php
<?php # -*- coding: utf-8 mode: php -*- vim:sw=2:sts=2:et:ai:si:sta:fenc=utf-8
if ($argc <= 1) die("ERROR: Vous devez spécifier le nom de la clé\n");
2020-03-26 16:40:37 +04:00
$argindex = 1;
$arg = $argv[1];
$sep = "\t";
if ($arg == "-s") {
$sep = $argv[2];
$argindex += 2;
} elseif (substr($arg, 0, 2) == "-s") {
$sep = substr($arg, 2);
$argindex++;
}
$datum = json_decode(stream_get_contents(STDIN), true);
$first = true;
2020-03-26 16:40:37 +04:00
for ($i = $argindex; $i < $argc; $i++) {
$result = $datum;
$keys = explode(".", trim($argv[$i]));
foreach ($keys as $key) {
if ($result === null) break;
2020-03-26 16:40:37 +04:00
if ($key === "") continue;
if (isset($result[$key])) {
$result = $result[$key];
} else {
$result = null;
}
}
if ($first) $first = false;
2020-03-26 16:40:37 +04:00
else echo $sep;
if (is_array($result)) var_export($result);
else echo $result;
}
echo "\n";