1

I am using json_encode to convert an array into JSON and then store in a MySQL table. While trying to do a json_decode on the data from the MySQL table, I get an error that the argument passed to json_decode is an object.

"json_decode() expects parameter 1 to be string, object given"

Here is the JSON string stored in MySQL table:

{  
   "CreditScoreFactor":"{\"98\":\" Make all future payments on time. The impact on your credit score from the bankruptcy will diminish over time.\",\"negative_factors\":[\" There is a bankruptcy on your credit report\",\" The balances on your accounts are too high compared to loan amounts\",\" Lack of sufficient relevant real estate account information\",\" You have either very few loans or too many loans with recent delinquencies\"],\"04\":\" Paying down the balances on your accounts will benefit your score.\",\"63\":\" Maintaining open and active credit accounts in good standing can help improve your credit score.\",\"08\":\" Paying bills on time every month is important to maintaining a good credit score. If you remain behind with any payments, bring them current as soon as possible, and then make future payments on time. Over time, this will have a positive impact on your score.\"}",
   "TotalBalances":"5039",
   "TotalMonthlyPayments":"57",
   "TotalAccounts":"4",
   "OpenAccounts":"3",
   "CloseAccounts":"1",
   "DelinquentAccounts":"0",
   "DerogatoryAccounts":"3",
   "PublicRecords":null,
   "Utilization":null,
   "OnTimePaymentPercentage":null,
   "BorrowerName":[  
      {  
         "first":"SOOR",
         "middle":"R",
         "last":"DOOR",
         "InquiryDate":"2019-03-06"
      }
   ],
   "BorrowerBirth":[  
      {  
         "date":null,
         "InquiryDate":[  
            "2019-03-06"
         ]
      }
   ],
   "previousAddresses":[  
      {  
         "dateReported":"2006-09-28",
         "InquiryDate":[  
            "2019-03-06"
         ],
         "address":{  
            "city":"SCOTTSDALE",
            "direction":"N",
            "houseNumber":"1001",
            "postDirection":"",
            "streetName":"27",
            "stateCode":"AK",
            "streetType":"PL",
            "unit":"105",
            "postalCode":"85257",
            "type":"previous"
         }
      },
      {  
         "dateReported":null,
         "InquiryDate":[  
            "2019-03-06"
         ],
         "address":{  
            "city":"PHOENIX",
            "direction":"E",
            "houseNumber":"4202",
            "postDirection":"",
            "streetName":"CACTUS",
            "stateCode":"AZ",
            "streetType":"RD",
            "unit":"4101",
            "postalCode":"85032",
            "type":"previous"
         }
      },
      {  
         "dateReported":"2007-10-09",
         "InquiryDate":[  
            "2019-03-06"
         ],
         "address":{  
            "city":"CALIFORNIA",
            "direction":"N",
            "houseNumber":"767",
            "postDirection":"",
            "streetName":"DAVID",
            "stateCode":"AZ",
            "streetType":"CT",
            "unit":"",
            "postalCode":"85226",
            "type":"current"
         }
      }
   ],
   "employer":[  
      {  
         "emp_updatedon":"2017-12-22",
         "emp_name":"PROEM PARTY EVENT RENTALS",
         "emp_partition":"0"
      },
      {  
         "emp_updatedon":"2007-04-27",
         "emp_name":"PROFESSIONAL EVENT MNGMNT",
         "emp_partition":"1"
      }
   ]
}

I noticed that if I remove backslashes then the error goes away. Also, if I wrap the database value in double quotes, the error goes away.

Not sure what the issue is. Do I need to wrap the json_encode() data with quotes before storing it in the database?

Using Laravel 5.7 with php 7.3.2

UPDATE: Here is the code that converts array to json

$reportData->extrainfo = !empty($report['extrainfo']) ? json_encode($report['extrainfo'],JSON_UNESCAPED_SLASHES) : null;
$reportData->save();

And here is the code for retreiving the DB value:

if ($extrainfo = $this->extrainfo) {
        $extrainfo = json_decode($extrainfo, true);
}
12
  • where is the code for While trying to do a json_decode on the data from mysql table and "json_decode() expects parameter 1 to be string, object given"this? Or any code. Commented Mar 9, 2019 at 4:33
  • you need to do is json_decode($json,true) ,When TRUE, returned objects will be converted into associative arrays. Commented Mar 9, 2019 at 4:33
  • 1
    @NullPoiиteя - that was what I thought at first, but this I get an error that the argument passed to json_decode is an object Probably the DB is returning stdClass objects. But no way to tell without the code. trying to do a json_decode on the data from mysql table Commented Mar 9, 2019 at 4:35
  • 1
    Looks like remnants of double encoding. And the error generally indicates that Eloquent implicitly decoded the ciolumn as per JSON declaration already. (Insufficient code shown here, of course.) Commented Mar 9, 2019 at 4:36
  • @ArtisticPhoenix Added the code that converts the array to json Commented Mar 9, 2019 at 4:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.