0

I have a string json that gives the following var dump:

"[[{"TransactionID":"00416","OrderID":"000926","TransactionOrderItemID":"001123","LastUpdate":"2009-10-28 13:03:31","CustomerID":"184","Company_name":"Test123","Invoiced":"0","SubItemsCount":"2","ProductID":"1","ProductTypesID":"1","ProductTypeName":"Phone","ProductName":"Phone Line (Home)","IncludePST":"1","BillType":"Monthly","BillingCycle":"Monthly","Status":"Active","CreationDate":"2009-10-28","ActivationStartDate":"2009-10-28","NextNotificationDate":"2009-10-27","OverWritePrice":"-1","PriceEconomic":"26.00","BasePrice":"26.0000","ProRate":"Yes","InvoicePrice":"3.35","ServicePeriod":" Pro-Rate: Oct-28-2009 - Oct-31-2009","EndDate":"2009-10-31"}]]"

When I try to decode as:

json_decode( $json, true);

The result is just null. $json is from db.
Note that I am only showing a subset of full data. The questions is, this is variable from db. If double quote is the problem, how to cast it?

5
  • 2
    I would suggest the problem lies in the part you omitted Commented Oct 28, 2009 at 18:25
  • Fair suggestion. But when I get this var dump, and put into a string, it decodes as expected. Commented Oct 28, 2009 at 18:27
  • 1
    That string decodes fine, so I agree with Greg. The problem is in the part that you did not post here. Commented Oct 28, 2009 at 18:27
  • how is the string getting stored into $json? I tried it with that string wrapped in single quote instead of double and json_decode() worked fine. Commented Oct 28, 2009 at 19:45
  • You are right Jim. If it is wrapped in single quote it works fine. But the trouble is how to do this automatically? Commented Oct 28, 2009 at 20:33

2 Answers 2

2

You can call json_last_error() to get more info about what went wrong.

For example this way:

json_decode($string);

switch(json_last_error())
{
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    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_NONE:
        echo ' - No errors';
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

1

It could be a problem with UTF-8 characters. Many posts on the json_decode manual page suggest that some conversion issues need to be handled.

Try using json_decode(utf8_encode($json));

1 Comment

How do you know string is in ISO-8859-1 encoding?

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.