1. Go to this page and download the library: Download eboubaker/json-finder 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/ */
eboubaker / json-finder example snippets
use Eboubaker\JSON\JSONFinder;
$html = file_get_contents('http://www.youtube.com');
$finder = JSONFinder::make();
/**
* @var \Eboubaker\JSON\JSONArray $foundEntries
*/
$foundEntries = $finder->findEntries($html);
// associative array of all found json entries
$associative = $foundEntries->assoc();
// 4th entry in the JSONArray
$entry = $foundEntries[3];
// get json string of the first entry (similar to json_encode)
$jsonString = strval($entry);
// loop through entries of the JSONArray ($foundEntries)
foreach($foundEntries as $key => $value) {
// ....
}
// loop through every deeply nested value(not object or array)
foreach($foundEntries->values() as $keyPath => $value) {
// ....
}
// pretty print the json entry with indentation of 2 spaces
echo $foundEntries->toReadableString(2);
use Eboubaker\JSON\JSONObject;
$obj = JSONObject::from([
"meta" => [
"id" => "12345",
"title" => "My Title",
],
"video" => [
"id" => "12345",
"formats" => [
[
"name" => "mp4",
"url" => "https://example.com/video720.mp4",
"resolution" => "1280x720",
],
[
"name" => "mp4",
"url" => "https://example.com/video1080.mp4",
"resolution" => "1920x1080",
],
[
"name" => "webm",
"url" => "https://example.com/video720.webm",
"resolution" => "1280x720",
],
]
]
]);
$obj->getAll('formats'); // empty array [], $obj does not contain 'formats' path
$result = $obj->getAll('meta.id'); // [0 => JSONValue("12345")]
$video_id = $result[0];
// get all deep entries that contains an 'id' key
$has_id = $obj->getAll('**.id');
// get all formats in 'video.formats' path
$all_formats = $obj->getAll('video.formats.*');
// you can apply a filter to the results
$mp4_formats = $obj->getAll('video.formats.*', fn($v) => $v->get('name')->equals('mp4')); // ['video.formats.0' => JSONObject({"name":"mp4","url":"https://example.com/video720.mp4","resolution":"1280x720"})]
// return paths of found results as the keys in the result array
$has_id = $obj->getAll('**.id', null, true); // ['meta.id' => JSONValue("12345"), 'video.id' => JSONValue("12345")]
use Eboubaker\JSON\JSONObject;
use Eboubaker\JSON\Contracts\JSONEntry;
$object = JSONObject::from([
"response" => [
"hash" => "a5339be0849ced1ffe",
"posts" => [
[
"id" => "1634",
"likes" => 700,
"text" => "Machine learning for beginners",
],
[
"id" => "1234",
"likes" => 200,
"text" => "top 10 best movies of 2019",
"comments" => [
[
"id" => "1134",
"likes" => 2,
"replies" => [],
"content" => "thanks for sharing",
],
[
"id" => "1334",
"content" => "this video is bad",
"likes" => 0,
"replies" => [
[
"id" => "1435",
"likes" => 0,
"content" => "this is not true",
],
[
"id" => "1475",
"likes" => 0,
"content" => "agree this is the worst",
]
],
]
],
]
]
]
]);
// get first object which matches these paths and filters.
$comment_with_likes = $object->search([
"content",
"likes" => fn(JSONEntry $v) => $v->value() > 0
]);
echo $comment_with_likes;// {"id":"1134","likes":2,"replies":[],"content":"thanks for sharing"}
$post_with_comment_replies = $object->search([
"comments.*.replies"
]);
echo $post_with_comment_replies['id'];// "1234"
// more than 0 replies
$comment_with_replies = $object->search([
"replies.*"
]);
echo $comment_with_replies['id'];// "1334"
$comment_with_bad_words = $object->search([
"content" => fn(JSONEntry $v) => $v->matches('/worst|bad/')
]);
echo $comment_with_bad_words['id'];// "1334"
use Eboubaker\JSON\JSONFinder;
// this finder can also find javascript objects/arrays
$finder = JSONFinder::make(JSONFinder::T_ALL_JSON | JSONFinder::T_JS);
$finder->findEntries("let data = {jskey: 1};")->first();// JSONObject({"jskey":1})
$finder->findEntries("var x = Object.freez({'js-key': 'js string'})")->first();// JSONObject({"js-key":"js string"})
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.