0

I am having the following JSON(invalid) as a string. I need to parse it and get the inner value. I tried a lot but didn't get the result.

    $sJson = '{"Place":"MyStore","PurchaseID":"IND.1234-5678-9012-34567","details":"{\"json\":\"{\\\"PurchaseID\\\":\\\"IND.1234-5678-9012-34567\\\",\\\"categoryname\\\":\\\"smartpack\\\",\\\"productname\\\":\\\"bitcoinpack1\\\",\\\"purchaseTime\\\":1504256011148,\\\"purchaseState\\\":\\\"Success\\\",\\\"UniqueToken\\\":\\\"jbbefjifdkpdpajfkomckoof.AO-J1OzEdsZX17M5pAvedDh1Ep_WwlOKamMQN_3O89bRbAPX-uoqPpTJf8EdNcjMhCK1dptGaWReUCSS9JGCJuh6GlAT0l11mkUddo_uJ4YOe8ezYxlmDQ8\\\"}\",\"currentvalue\":\"S270U2J3XF\\/+XnC1ocPp0d\\/Kwf\\/4B\\/\\/tT7urbDn6F+\\/D8j7VD1t8qqwevtKDnAafAtvocPg4Eevkf\\/GZKl1YOgUYyuY63nyekz7GRDuIKVXAZ+iZtPAbwCuwZplUQHaVA\\/EBMjYpPQM0EFtp2WuX\\/Tx9nTnFCtU+gAK4Rg0zLvQNKSJx5WfqhK7wf0wHTTYviTkB\\/pETnkV22oQDIZH9\\/Fy1FXltC7FXHXoMcxtGvkgPSEFOnms4HumjUQ5PtQUbxh\\/oirQeROCAhkO+WKX9WO3bCKjru1uuxspTLCNGJEKAezi2GEBcpFGjq4iS5N7SfO5BOF76\\/joLe3B7OemJNw==\"}"}';

    $sJson = json_encode($sJson,JSON_UNESCAPED_SLASHES);
    $json_array = (array) json_decode($sJson,true);
    $newjson=preg_replace('/.+?({.+}).+/','$1',$json_array);
    var_dump($newjson);
    exit;

The Result $newjson is not return as JSON Object, it gives as only string.

I need JSON object not a string.

In the given JSON, json value is a string(inside valid JSON) which starts with ".

I need to remove this.

9
  • benalman.com/news/2010/03/theres-no-such-thing-as-a-json Commented Sep 1, 2017 at 11:24
  • 1
    Why do you json_encode() something that is already encode? Don't you mean json_decode()? And I would use different variable names for the argument and result of this function. Commented Sep 1, 2017 at 11:24
  • What you have is invalid JSON... Commented Sep 1, 2017 at 11:27
  • It seems to be valid json at jslint.com and jsonlint.com, so you should be able to decode it. Commented Sep 1, 2017 at 11:28
  • at jslint maybe but not at JSONLint. Commented Sep 1, 2017 at 11:28

2 Answers 2

2

json_encode takes a PHP data structure and turns it into a string of JSON.

You need json_decode to go the other way.

Sign up to request clarification or add additional context in comments.

1 Comment

Given string is not a valid. Kindly check with online json editors.
1

The string is mostly valid json, you just need to tweak a few things:

First, replace all of the \\" with ". (I had to run this twice)

$string = str_replace('\\"', '"', $string);

Then some of the braces are quoted:

$string = str_replace('"{', '{', $string);
$string = str_replace('}"', '}', $string);

Once that was done, I was able to successfully decode your json. This could possibly be done faster with preg_replace, but I suck at regexes.

1 Comment

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.