m1ke/json-explore

Allows you to analyse the fields present in a data structure, aimed at APIs returning data as JSON

0.2.0 2019-07-09 13:43 UTC

This package is auto-updated.

Last update: 2024-11-10 01:02:17 UTC


README

For JSON or other APIs

Sometimes you get a response from an API or other data source which is structured data but the structure may not be obvious, e.g. 10 objects and their data differs slightly. Documentation may help but often can be incomplete or not even present.

This library is a utility to take a block of data that can translate into a PHP associative array and give you a breakdown of the possible keys that exist and base types that their values can take.

Install it

composer require m1ke/json-explore

See an example of it working

Run php example/example.php

Use it

Three ways to create the object:

JsonExplore::fromJson($json);
JsonExplore::fromArray($arr);
JsonExplore::fromObj($basic_object); // casts to array, pretty basic

Do the fun bit:

$json_explore->analyse();

Output some data:

$json_explore->dump(); // var_dumps the analysis
echo $json_explore->asJson(); // pretty printed JSON object of the analysis
echo $json_explore->asPathString(); // list of keys split with dot notation

Potential improvements

  • Unit tests (basically the example but as a PhpUnit test)
  • Output JMESPATH or other targetting syntax for specific keys
  • Other data inference, e.g. email, phone number, url