9

I'm having troubles getting json_decode to work on a specific string I am receiving.

I have narrowed it down to this line:

"systemNotes[6]": "January 09, 2013 12:52 PM - Test Name - Changed Billing Address 2 From to Shipping First Name: Shipping Last Name: Email Address: Shipping Address: Shipping Address 2: Shipping City: Shipping Zip/Postal: Shipping Country: Shipping State: Phone: Billing First Name: Billing Last Name: Billing Address: Billing Address 2: Billing C"

Copying the json from this question, the problem is not reproducible - but a representative snippet of the original json is here: http://codepad.org/ZzrC7rqQ - and putting that in jsonlint.com gives:

Parse error on line 3:
...  "systemNotes[6]": "January 09, 2013 12
-----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

What is wrong with this string such that it's invalid json?

EDIT

I managed to find the exact code coming across.

"systemNotes[6]":"January+09%2C+2013+12%3A52+PM+-+First+Name+-+Changed++Billing+Address+2+From++to+Shipping+First+Name%3A%09+Shipping+Last+Name%3A%09+Email+Address%3A%09+Shipping+Address%3A+%09+Shipping+Address+2%3A+%09+Shipping+City%3A+%09+Shipping+Zip%2FPostal%3A+%09+Shipping+Country%3A+%09+Shipping+State%3A+%09+Phone%3A+%09+Billing+First+Name%3A+%09+Billing+Last+Name%3A+%09+Billing+Address%3A+%09+Billing+Address+2%3A+%09+Billing+C"

This seems to be ok so maybe the problem is coming from when I do the parse_str, here's the code I am using:

$response = apiConnection($data);
parse_str($response, $parse);
$each = json_decode($parse['data']);
foreach($each as $key => $order){
   //do something
}
14
  • How are you collecting the string? Commented Jul 19, 2013 at 18:20
  • I am querying an API and it is sending it back to me as a string. Which I am then using a parse_str on which leaves it in an array. Commented Jul 19, 2013 at 18:21
  • 1
    Could you check if the string has non-UTF 8 characters? Commented Jul 19, 2013 at 18:25
  • 1
    THanks for that link AD7six: codepad.org/ZzrC7rqQ Commented Jul 19, 2013 at 19:05
  • 1
    Out of curiosity, how is the passed string generated? Is it under your control? Commented Jul 24, 2013 at 23:52

2 Answers 2

11
+50

The problem is that tab characters are not valid inside a string.

Removing the tab characters like here http://codepad.org/8fnQphkS and using that at jsonlint.com you will see it see now valid json.

Have a look at the specs for JSON at http://www.ietf.org/rfc/rfc4627.txt?number=4627 specially section 2.5 where the tab character is called out by name as one of the characters that must be escaped if inside a string.

EDIT:

Here is a way of stripping out all tabs and multiple spaces and replacing them with a single space character:

$data = preg_replace('/[ ]{2,}|[\t]/', ' ', trim($data));
Sign up to request clarification or add additional context in comments.

3 Comments

He would be better off decoding the json before he removes the url encoding, though.
$data = preg_replace('/[\s]+|[\t]/', ' ', trim($data)); - all types of whitespace
If you are going to proceed with mutating the full string, then just replace one or more whitespace characters with a single literal space. '/\s+/', ' '
6

did you try something like that? it would help to clean your string

$yourstring = preg_replace('/[^(\x20-\x7F)]*/','', $yourstring);

Comments

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.