Here is one of the samples of Claude AI JSON response
"""
Here is the summary and score in the requested JSON format:
<response>
{"653b2f2f90f8cb084803e4c6": {"summary": "The company manufactures equipment rentals based on the services listed on page 1.", "ai_score": 100}, "653b2f2f90f8cb084803e4c7": {"summary": "No evidence of a corporate social responsibility plan or policy was found in the text.", "ai_score": 0}, "653b2f2f90f8cb084803e4c8": {"summary": "No evidence of a cyber security management plan or policy was found in the text.", "ai_score": 0}, "653b2f2f90f8cb084803e4c9": {"summary": "No evidence was found regarding work in MSHA regulated sites.", "ai_score": 0}, "653b2f2f90f8cb084803e4ca": {"summary": "No evidence was found regarding operation of commercial motor vehicles.", "ai_score": 0}, "653b2f2f90f8cb084803e4cb": {"summary": "No evidence was found regarding DOT operator qualifications for pipeline tasks.", "ai_score": 0}, "653b2f2f90f8cb084803e4cc": {"summary": "The company owns, rents, leases, or subcontracts mobile cranes and lifting equipment based on the services listed on page 1.", "ai_score": 100}, "653b2f2f90f8cb084803e4cd": {"summary": "No evidence was found regarding required environmental licenses or permits.", "ai_score": 0}, "653b2f2f90f8cb084803e4ce": {"summary": "No evidence was found regarding work in PSM facilities.", "ai_score": 0}, "653b2f2f90f8cb084803e4cf": {"summary": "No evidence was found regarding transport of hazardous materials.", "ai_score": 0}, "653b2f2f90f8cb084803e4d0": {"summary": "No evidence was found regarding bringing chemicals onto owner/client sites.", "ai_score": 0}, "653b2f2f90f8cb084803e4d1": {"summary": "No evidence was found regarding use of temporary labor/leasing agencies.", "ai_score": 0}, "653b2f2f90f8cb084803e4d2": {"summary": "No evidence was found regarding use of subcontractors.", "ai_score": 0}, "653b2f2f90f8cb084803e4d3": {"summary": "No evidence was found regarding union craft labor.", "ai_score": 0}, "653b2f2f90f8cb084803e4d4": {"summary": "No evidence was found regarding substance abuse testing.", "ai_score": 0}, "653b2f2f90f8cb084803e4d5": {"summary": "No evidence was found regarding high-risk work activities performed.", "ai_score": 0}, "653b2f2f90f8cb084803e4d7": {"summary": "No evidence was found of third party certification of a safety management system.", "ai_score": 0}, "653b2f2f90f8cb084803e4d8": {"summary": "No evidence was found regarding following ISO 45001 best practices for safety processes.", "ai_score": 0}, "653b2f2f90f8cb084803e4d9": {"summary": "No evidence was found of third party certification of an environmental management system.", "ai_score": 0}, "653b2f2f90f8cb084803e4da": {"summary": "No evidence was found regarding following ISO 14001 best practices for environmental policies.", "ai_score": 0}, "653b2f2f90f8cb084803e4db": {"summary": "No evidence was found of third party certification of a quality management system.", "ai_score": 0}, "653b2f2f90f8cb084803e4dc": {"summary": "No evidence was found regarding following ISO 9001 best practices for quality policies.", "ai_score": 0}, "653b2f2f90f8cb084803e4df": {"summary": "No evidence was found regarding authorization of health, safety and environmental policies by senior leadership.", "ai_score": 0}, "653b2f2f90f8cb084803e4e0": {"summary": "No evidence was found regarding frequency of manager/executive presence on worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e4e1": {"summary": "No evidence was found regarding managers/executives participating in safety activities on worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e4e2": {"summary": "No evidence was found regarding processes for subcontractor engagement and management regarding health, safety and environment.", "ai_score": 0}, "653b2f2f90f8cb084803e4e3": {"summary": "No evidence was found regarding frequency of manager/executive participation in safety meetings.", "ai_score": 0}, "653b2f2f90f8cb084803e4e4": {"summary": "No evidence was found of a health, safety and environment policy statement signed by top management.", "ai_score": 0}, "653b2f2f90f8cb084803e4e6": {"summary": "No evidence was found regarding a permit to work system.", "ai_score": 0}, "653b2f2f90f8cb084803e4e7": {"summary": "No evidence was found regarding a substance abuse and testing policy.", "ai_score": 0}, "653b2f2f90f8cb084803e4e8": {"summary": "No evidence was found regarding work rules prohibiting impairment by alcohol, drugs and medications.", "ai_score": 0}, "653b2f2f90f8cb084803e4e9": {"summary": "No evidence was found regarding types of substance abuse testing performed.", "ai_score": 0}, "653b2f2f90f8cb084803e4ea": {"summary": "No evidence was found regarding criminal background checks.", "ai_score": 0}, "653b2f2f90f8cb084803e4eb": {"summary": "No evidence was found regarding a policy prohibiting weapons on worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e4ec": {"summary": "No evidence was found regarding a waste management plan or policy.", "ai_score": 0}, "653b2f2f90f8cb084803e4ed": {"summary": "No evidence was found of a health, safety and environment manual covering regulations.", "ai_score": 0}, "653b2f2f90f8cb084803e4ef": {"summary": "No evidence was found regarding assignment of health, safety and environment roles and responsibilities.", "ai_score": 0}, "653b2f2f90f8cb084803e4f0": {"summary": "No evidence was found regarding who is responsible for coordinating the health, safety and environment program.", "ai_score": 0}, "653b2f2f90f8cb084803e4f1": {"summary": "No evidence was found regarding communication of the health, safety and environment policy.", "ai_score": 0}, "653b2f2f90f8cb084803e4f3": {"summary": "No evidence was found regarding employees receiving their own copy of a health, safety and environment handbook.", "ai_score": 0}, "653b2f2f90f8cb084803e4f4": {"summary": "No evidence was found regarding a method for communicating with employees about health, safety and environment matters.", "ai_score": 0}, "653b2f2f90f8cb084803e4f5": {"summary": "No evidence was found regarding a health, safety and environment committee with worker representation.", "ai_score": 0}, "653b2f2f90f8cb084803e4f6": {"summary": "No evidence was found regarding including subcontractors in onsite activities.", "ai_score": 0}, "653b2f2f90f8cb084803e4f9": {"summary": "No evidence was found regarding a process to determine legal requirements and changes related to health, safety and environment programs.", "ai_score": 0}, "653b2f2f90f8cb084803e4fa": {"summary": "No evidence was found regarding a system to control hazardous materials brought to, used, and removed from worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e4fb": {"summary": "No evidence was found regarding documented processes for regular health, safety and environment inspections at workplaces.", "ai_score": 0}, "653b2f2f90f8cb084803e4fc": {"summary": "No evidence was found regarding safe work procedures for high-risk work activities.", "ai_score": 0}, "653b2f2f90f8cb084803e4fd": {"summary": "No evidence was found regarding processes for ongoing, proactive hazard identification and risk control.", "ai_score": 0}, "653b2f2f90f8cb084803e4fe": {"summary": "No evidence was found regarding onsite and equipment inspections.", "ai_score": 0}, "653b2f2f90f8cb084803e4ff": {"summary": "No evidence was found regarding a preventative maintenance program for tools and equipment.", "ai_score": 0}, "653b2f2f90f8cb084803e500": {"summary": "No evidence was found regarding job safety analysis or equivalent.", "ai_score": 0}, "653b2f2f90f8cb084803e501": {"summary": "No evidence was found regarding a documented field observation program.", "ai_score": 0}, "653b2f2f90f8cb084803e502": {"summary": "No evidence was found regarding an industrial hygiene or occupational health monitoring program.", "ai_score": 0}, "653b2f2f90f8cb084803e503": {"summary": "No evidence was found regarding a written hearing conservation program.", "ai_score": 0}, "653b2f2f90f8cb084803e504": {"summary": "No evidence was found regarding fitness work exams.", "ai_score": 0}, "653b2f2f90f8cb084803e505": {"summary": "No evidence was found regarding provision of personal protective equipment.", "ai_score": 0}, "653b2f2f90f8cb084803e506": {"summary": "No evidence was found regarding procedures to manage and coordinate health, safety and environment risks on multi-employer worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e507": {"summary": "No evidence was found regarding a risk matrix to quantify hazards.", "ai_score": 0}, "653b2f2f90f8cb084803e508": {"summary": "No evidence was found regarding a process to identify risks in early planning and design stages.", "ai_score": 0}, "653b2f2f90f8cb084803e509": {"summary": "No evidence was found regarding a documented hazard register.", "ai_score": 0}, "653b2f2f90f8cb084803e50c": {"summary": "No evidence was found regarding budgeting for the health, safety and environment management system.", "ai_score": 0}, "653b2f2f90f8cb084803e50d": {"summary": "No evidence was found regarding provision of full-time onsite health, safety and environment representation for projects.", "ai_score": 0}, "653b2f2f90f8cb084803e50e": {"summary": "No evidence was found regarding a formal health, safety and environment department/group structure.", "ai_score": 0}, "653b2f2f90f8cb084803e510": {"summary": "No evidence was found regarding resumes/qualifications for project supervision and safety management.", "ai_score": 0}, "653b2f2f90f8cb084803e511": {"summary": "No evidence was found regarding a method to ensure personnel are competent to perform work activities.", "ai_score": 0}, "653b2f2f90f8cb084803e512": {"summary": "No evidence was found regarding training supervisors in health, safety and environment fundamentals.", "ai_score": 0}, "653b2f2f90f8cb084803e513": {"summary": "No evidence was found regarding frequency of onsite safety meetings.", "ai_score": 0}, "653b2f2f90f8cb084803e514": {"summary": "No evidence was found regarding including environmental topics in safety meetings.", "ai_score": 0}, "653b2f2f90f8cb084803e515": {"summary": "No evidence was found regarding provision of health, safety and environment training to supervisors.", "ai_score": 0}, "653b2f2f90f8cb084803e516": {"summary": "No evidence was found regarding the percentage of supervisors/managers trained in OSHA 10-hour construction.", "ai_score": 0}, "653b2f2f90f8cb084803e517": {"summary": "No evidence was found regarding the percentage of supervisors/managers trained in OSHA 30-hour construction.", "ai_score": 0}, "653b2f2f90f8cb084803e519": {"summary": "No evidence was found regarding how workers are informed of their right to stop dangerous work.", "ai_score": 0}, "653b2f2f90f8cb084803e51a": {"summary": "No evidence was found regarding tracking of stop work authority events.", "ai_score": 0}, "653b2f2f90f8cb084803e51b": {"summary": "No evidence was found regarding updating employees on changes influencing procedures.", "ai_score": 0}, "653b2f2f90f8cb084803e51c": {"summary": "No evidence was found regarding employees' ability to read, write and understand English.", "ai_score": 0}, "653b2f2f90f8cb084803e51d": {"summary": "No evidence was found regarding ensuring training records and competency evidence are available at worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e51e": {"summary": "No evidence was found regarding a document control procedure for health, safety and environment records.", "ai_score": 0}, "653b2f2f90f8cb084803e521": {"summary": "No evidence was found regarding a written post-injury management training program.", "ai_score": 0}, "653b2f2f90f8cb084803e522": {"summary": "No evidence was found regarding use of medical providers that understand return-to-work programs.", "ai_score": 0}, "653b2f2f90f8cb084803e523": {"summary": "No evidence was found regarding a case management policy/procedure for returning injured workers to duty.", "ai_score": 0}, "653b2f2f90f8cb084803e524": {"summary": "No evidence was found regarding a written CPR training program and documentation.", "ai_score": 0}, "653b2f2f90f8cb084803e525": {"summary": "No evidence was found regarding employment of medical personnel at large worksites.", "ai_score": 0}, "653b2f2f90f8cb084803e526": {"summary": "No evidence was found regarding routine rescue drills.", "ai_score": 0}, "653b2f2f90f8cb084803e527": {"summary": "No evidence was found regarding spill kits and cleanup supplies.", "ai_score": 0}, "653b2f2f90f8cb084803e528": {"summary": "No evidence was found regarding a process to confirm employees are free of COVID-19 symptoms before site access.", "ai_score": 0}, "653b2f2f90f8cb084803e529": {"summary": "No evidence was found regarding procedures if an employee tests positive for COVID-19.", "ai_score": 0}, "653b2f2f90f8cb084803e52a": {"summary": "No evidence was found regarding a method for COVID-19 contact tracing.", "ai_score": 0}, "653b2f2f90f8cb084803e52b": {"summary": "No evidence was found regarding an emergency response management procedure.", "ai_score": 0}, "653b2f2f90f8cb084803e52d": {"summary": "No evidence was found regarding standard operating procedures for work activities.", "ai_score": 0}, "653b2f2f90f8cb084803e52f": {"summary": "No evidence was found regarding a short service employee policy.", "ai_score": 0}, "653b2f2f90f8cb084803e530": {"summary": "No evidence was found regarding a written substance abuse awareness program.", "ai_score": 0}, "653b2f2f90f8cb084803e531": {"summary": "No evidence was found regarding a documented process for management of change.", "ai_score": 0}, "653b2f2f90f8cb084803e532": {"summary": "No evidence was found regarding a permit to work system for high risk activities.", "ai_score": 0}, "
"""
and here is my code:
preg_match('/\{(?:[^{}]|(?R))*\}/x', $openAiResponse, $matches);
$openAiResponse = $matches[0] ?? [];
$cleanedResponse = preg_replace('/[[:cntrl:]](?!\r\n)/', '', $openAiResponse);
$decoded = json_decode($cleanedResponse, true);
It used to work until now but with the particular response mentioned above, it is throwing an error
Exception: json_decode() expects parameter 1 to be string, array given
Because in this particular case $matches is returning as an empty array [].
Basically, with the first regular expression, I am trying to get only those text inside the tag. With another regular expression, it filters other control characters except for line breaks so that it doesn't throw an error while json_decoding.
Recently it has been working fine until now, however, in the past as well I had some issues occassionally because of some characters in response. So I am trying to find one permanent solution to these which I can implement with confidence, and also fix current json_decode issues.