How to Validate a JSON File in PHP
1 min readApr 24, 2020
Having a system in place for JSON validation is like having a guardian angel that gets you out of jams before you even knew they were there. So how can we create said system without wasting an entire afternoon to its creation? The answer is simple: we are going to use an API.
So we begin with setup; just run this command from the command line of composer.
composer require cloudmersive/cloudmersive_document_convert_api_client
With access available to our API, we can call upon validateDocumentJsonValidation with this snippet:
<?phprequire_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed// $config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKeyPrefix('Apikey', 'Bearer');$apiInstance = new Swagger\Client\Api\ValidateDocumentApi(// If you want use custom http client, pass your client which implements `GuzzleHttp\ClientInterface`.// This is optional, `GuzzleHttp\Client` will be used as default.new GuzzleHttp\Client(),$config);$input_file = "/path/to/file"; // \SplFileObject | Input file to perform the operation on.try {$result = $apiInstance->validateDocumentJsonValidation($input_file);print_r($result);} catch (Exception $e) {echo 'Exception when calling ValidateDocumentApi->validateDocumentJsonValidation: ', $e->getMessage(), PHP_EOL;}?>
And there you have it, folks. We have our JSON validation solution!