troubleshooting Question

PHP JSON_Decode() Error Detection

Avatar of Ray Paseur
Ray PaseurFlag for United States of America asked on
JavaScriptPHPAJAX
10 Comments1 Solution2913 ViewsLast Modified:
PHP JSON error handling does not give much of a clue as to what is wrong with this JSON string, and it's awfully long to try to deconstruct it manually.  Does anyone have an automated method that will detect and show the point of the error?

Thanks and regards, ~Ray

<?php // RAY_temp_badjson.php
error_reporting(E_ALL);

$jso = <<<EOD
{"LogonTokenAccepted":"true","Success":"true","ExceptionMessage":"","MessageSentTime":"2013-07-02T14:55:33.1914783+01:00","Notes":"","QuoteRef":"40802717","NNoResponses":"0","ResponseItemArray":[{"ResponseRef":"415503208","ResponseGuid":"c5c59148-1b52-4f8e-9571-9ce31a9c74f3","ErrorCode":"SUCCESS","ProviderID":"79","ProviderIdent":"BGL","ProviderName":"Beagle Street","ProductID":"1229","ProvidersProductID":"TBA","ProductName":"Level term","Info":"1","NotesHTML":"","DocumentArray":[{"Description":"Policy Summary","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=2214923&Guid=7ce6d566-a083-4f82-90f8-7e85808e2d94","DocumentType":"KFD"},{"Description":"Policy Terms and Conditions","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=2214927&Guid=3ba04a6d-e087-4393-982e-a807d20abd78","DocumentType":"Terms of Business \/ Initial Disclosure Document"}],"ElectronicApplicationAvailable":"true","ProviderLogoAbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument.aspx?DocType=104&Path=Images\/Providers\/BGL.png","CommissionInitial":"0","CommissionRenewal":"1.1808","CommissionFreeText":"","CommissionInitialTerm":"-1","CommissionFrequency":"UNDEFINED","CommissionIndemnity":"UNDEFINED","Benefit":"100000","Premium":"6.56","NetPremium":"6.56","CICTPDDef":"see illustration","CICTPDMask":"0","Rates":"Guaranteed","LoanRateString":""},{"ResponseRef":"415503207","ResponseGuid":"a5e0241a-44bb-4975-9ada-c9ddccde7d0e","ErrorCode":"WARNING","ProviderID":"11","ProviderIdent":"LANDG","ProviderName":"Legal & General","ProductID":"25","ProvidersProductID":"AAA1","ProductName":"Term - Electronic","Info":"1","NotesHTML":"1. First\/Second Client Occupation not recognised - assumed default occupation class 12. TIC included automatically3. Quote produced based on electronic submission. Please apply online4. Quotation has been generated using today's date","DocumentArray":[{"Description":"Mort and Fam Prot KFD with conversion","AbsoluteURL":"http:\/\/www.legalandgeneral.com\/library\/protection\/key-features\/W13556.pdf","DocumentType":"KFD"},{"Description":"OLPC Data Application Form Family & Mortgage","AbsoluteURL":"http:\/\/www.legalandgeneral.com\/library\/protection\/application-form\/W11904.pdf?ResponseRef=415503207","DocumentType":"Proposal"},{"Description":"OLPC Data Capture Form Interactive Underwriting Family & Mortgage","AbsoluteURL":"http:\/\/www.legalandgeneral.com\/library\/protection\/application-form\/W13433.pdf?ResponseRef=415503207","DocumentType":"Data Capture Form"}],"ElectronicApplicationAvailable":"true","ProviderLogoAbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument.aspx?DocType=104&Path=Images\/Providers\/landg.png","CommissionInitial":"0","CommissionRenewal":"0","CommissionFreeText":"","CommissionInitialTerm":"48","CommissionFrequency":"Monthly","CommissionIndemnity":"UNDEFINED","Benefit":"100000","Premium":"6.99","NetPremium":"6.99","CICTPDDef":"see illustration","CICTPDMask":"0","Rates":"Guaranteed","LoanRateString":""},{"ResponseRef":"415503206","ResponseGuid":"6cb8576c-483c-4427-bf60-48f1c49c6d26","ErrorCode":"WARNING","ProviderID":"6","ProviderIdent":"ZURICH","ProviderName":"Zurich","ProductID":"786","ProvidersProductID":"DY_P","ProductName":"Level Protection Plan (Gtd) - Paper","Info":"1","NotesHTML":"1. The illustration assumes a commencement date of 01-08-20132. Commission accrual period set to '4' years in line with Zurich agreement3. When applying please ensure you have an Agency Agreement with Zurich that matches the selected commission profile.4. Terminal Illness Included.","DocumentArray":[{"Description":"Key features of the Level Protection Plan\n\u2013 Guaranteed Payments","AbsoluteURL":"http:\/\/zdownload.zurich.co.uk\/document\/pdf\/zurich\/kfds\/123331.pdf","DocumentType":"KFD"},{"Description":"Level Protection Plan (Gtd) Application Form","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=172183&Guid=64d7b402-3cb9-494a-8c03-1350bbcb449a&ResponseRef=415503206","DocumentType":"Proposal"},{"Description":"Protection Life of Another Application","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=628590&Guid=097ee87d-6deb-4f25-ba2c-15309681af50&ResponseRef=415503206","DocumentType":"Proposal"},{"Description":"Data Capture Form","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=628494&Guid=b9a9822f-b809-4228-8673-d91a1b76ca9f&ResponseRef=415503206","DocumentType":"Data Capture Form"}],"ElectronicApplicationAvailable":"false","ProviderLogoAbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument.aspx?DocType=104&Path=Images\/Providers\/zurich.gif","CommissionInitial":"0","CommissionRenewal":"-1","CommissionFreeText":"","CommissionInitialTerm":"-1","CommissionFrequency":"UNDEFINED","CommissionIndemnity":"UNDEFINED","Benefit":"100000","Premium":"7.34","NetPremium":"7.34","CICTPDDef":"see illustration","CICTPDMask":"0","Rates":"Guaranteed","LoanRateString":""},{"ResponseRef":"415503209","ResponseGuid":"3eac2bd5-038c-45ff-84ff-48130f70a866","ErrorCode":"SUCCESS","ProviderID":"72","ProviderIdent":"Ageas","ProviderName":"Ageas Protect","ProductID":"907","ProvidersProductID":"TA","ProductName":"Term Protection","Info":"1","NotesHTML":"","DocumentArray":[{"Description":"Key Facts (Critical Illness)","AbsoluteURL":"http:\/\/www.ageasprotect.co.uk\/Document\/0874_YourLifePlan_CI_KF","DocumentType":"KFD"},{"Description":"YourLife Plan (Term Assurance) Cover Details","AbsoluteURL":"http:\/\/www.ageasprotect.co.uk\/Document\/0882_YourLifePlan_TA_CD","DocumentType":"TechnicalGuide"},{"Description":"YourLife Plan (Critical Illness) Cover Details","AbsoluteURL":"http:\/\/www.ageasprotect.co.uk\/Document\/0883_YourLifePlan_CI_CD","DocumentType":"TechnicalGuide"},{"Description":"Personal Data Capture Form","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=1383761&Guid=5921d1ff-9218-4200-9978-7e120f286025&ResponseRef=415503209","DocumentType":"Data Capture Form"},{"Description":"Key Facts (Term Assurance)","AbsoluteURL":"http:\/\/www.ageasprotect.co.uk\/Document\/0873_YourLifePlan_TA_KF","DocumentType":"KFD_LIFE"}],"ElectronicApplicationAvailable":"true","ProviderLogoAbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument.aspx?DocType=104&Path=Images\/Providers\/ageas.png","CommissionInitial":"0","CommissionRenewal":"0","CommissionFreeText":"","CommissionInitialTerm":"48","CommissionFrequency":"Monthly","CommissionIndemnity":"UNDEFINED","Benefit":"100000","Premium":"7.6","NetPremium":"7.6","CICTPDDef":"see illustration","CICTPDMask":"0","Rates":"Guaranteed","LoanRateString":""},{"ResponseRef":"415503211","ResponseGuid":"a88c40df-b29d-4570-b15f-59fa13aadf61","ErrorCode":"WARNING","ProviderID":"2","ProviderIdent":"NU","ProviderName":"Aviva","ProductID":"16","ProvidersProductID":"TN-P","ProductName":"Term Assurance","Info":"1","NotesHTML":"1. Unrecognised vendor post code.","DocumentArray":[{"Description":"Term Assurance Key Facts","AbsoluteURL":"http:\/\/aviva.co.uk\/adviser\/product-literature\/view-document.cgi?f=pt01002.pdf","DocumentType":"KFD"},{"Description":"Term Assurance Application Form","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=75892&Guid=ab442381-13c9-4c86-a000-6f7ffccdfda0&ResponseRef=415503211","DocumentType":"Proposal"},{"Description":"Term Assurance - Terms and Conditions","AbsoluteURL":"http:\/\/aviva.co.uk\/adviser\/product-literature\/view-document.cgi?f=pt01776.pdf","DocumentType":"General Document"},{"Description":"Term Assurance - At a Glance.","AbsoluteURL":"http:\/\/www.aviva.co.uk\/adviser\/life-insurance-documents\/view-document.cgi?f=pt01784c.pdf&lid=prod-lit-Term%20Assurance&lpos=pt01784c.pdf","DocumentType":"General Document"},{"Description":"Protection Declaration for electronic business","AbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument2.aspx?DocID=123027&Guid=d4dfcc21-ca64-4d9e-a127-0cb158983b97","DocumentType":"Declaration"}],"ElectronicApplicationAvailable":"false","ProviderLogoAbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument.aspx?DocType=104&Path=Images\/Providers\/NU.gif","CommissionInitial":"-1","CommissionRenewal":"-1","CommissionFreeText":"No commission will be paid","CommissionInitialTerm":"-1","CommissionFrequency":"UNDEFINED","CommissionIndemnity":"UNDEFINED","Benefit":"100000","Premium":"7.7","NetPremium":"7.7","CICTPDDef":"see illustration","CICTPDMask":"0","Rates":"Guaranteed","LoanRateString":""},{"ResponseRef":"415503210","ResponseGuid":"5b7b21d4-7080-476c-a485-1519f4823fd0","ErrorCode":"SUCCESS","ProviderID":"18","ProviderIdent":"SCOTLIFE","ProviderName":"Bright Grey","ProductID":"1010","ProvidersProductID":"lifestyle","ProductName":"Apply online - Lifestyle Protection","Info":"1","NotesHTML":"","DocumentArray":[{"Description":"Key Facts","AbsoluteURL":"http:\/\/www.brightadviser.co.uk\/downloads\/PC1409C.key-facts-lifestyle-protection.pdf","DocumentType":"KFD"},{"Description":"Plan Details Booklet","AbsoluteURL":"http:\/\/www.brightadviser.co.uk\/downloads\/PC1408C.plan-details-lifestyle-protection.pdf","DocumentType":"Brochure"}],"ElectronicApplicationAvailable":"true","ProviderLogoAbsoluteURL":"https:\/\/www.webline.co.uk\/WeblineNet\/GetDocument.aspx?DocType=104&Path=Images\/Providers\/scotlife.jpg","CommissionInitial":"0","CommissionRenewal":"0.2","CommissionFreeText":"","CommissionInitialTerm":"-1","CommissionFrequency":"Monthly","CommissionIndemnity":"UNDEFINED","Benefit":"100000","Premium":"7.86","NetPremium":"7.86","CICTPDDef":"see illustration","CICTPDMask":"0","Rates":"Guaranteed","LoanRateString":""}]}
EOD;

// TRY TO DECODE AND CHECK FOR ERRORS
$obj = json_decode($jso);
switch (json_last_error())
{
    case JSON_ERROR_NONE:
        echo ' - No errors';
    break;
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Underflow or the modes mismatch';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
    default:
        echo ' - Unknown error';
    break;
}

var_dump($obj);
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros