1

This is a non standard json format api :

   WebInfo ({
    "name": "Google",
    "code": 1,
    "domain": "http://www.google.com/",
    })

as the data is not a standard JSON, json_decode() can not be resolved directly, what can I do to make it become a valid json format ?

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
StackSN
  • 13
  • 6

2 Answers2

2

You will have to strip out WebInfo ( and ) which can be done by using str_replace()

Try this: Lets assume your string that has WebInfo({...}) is $str then do this:

$str = str_replace('WebInfo(', '', $str); 
$str = str_replace(' )', '', $str);
print_r(json_decode($str, true));

WARNING: str_replace() will strip out every character mentioned from the string. Eg: if I use something like

str_replace('hello', 'hi', $str);

Then it will replace every hello to hi in $str.

Aniket Sahrawat
  • 12,410
  • 3
  • 41
  • 67
  • This will end in some funny results as soon as you have something like `MusicInfoCallback(` or ` )` in the name or the domain fields or as soon as there is no leading space before the ")" (e.g. caused by different line breaks on different systems). `str_replace` is definitely **not** the right tool for this task. – Quasdunk Nov 27 '16 at 07:10
  • According to [this](https://s007-outspa74225.c9users.io/test.php), there is a space before `)` @Quasdunk – Aniket Sahrawat Nov 27 '16 at 07:11
  • @AniketSahrawat and according to what there is never a " )" in the name or domain or whatever else there migh be? – Quasdunk Nov 27 '16 at 07:18
  • I agree with you but in this case there is no other element enclosed inside `()` otherwise you will need `regex` for removing and that is why i upvoted your answer @Quasdunk – Aniket Sahrawat Nov 27 '16 at 07:22
  • Or, just use `substr`, because that's exactly the purpose of it. `str_replace` is for, well, *replacing strings*, `substr` is for extracting a substring - which is exactly what you want to do. Both tools serve different purposes, so why on earth would you eat a soup with a fork (which may work) if there is a spoon right next to you? Your answer may work in this particular case, but then you at least should mention the caveats! – Quasdunk Nov 27 '16 at 07:31
  • @Quasdunk I thought that is already mentioned in your answer and hence no need to show it in a separate answer. You can make the changes to my answer if you want. Just watch for extra spaces after `)` if there are any – Aniket Sahrawat Nov 27 '16 at 07:37
  • I'm not talking about any caveats in my answer, because with those approaches you don't need to care about it. I'm talking about the outcome of your code if e.g. the `name` was "Google ( my favorite search engine )". You would actually alter the payload data (!!!) which is absolutely not what you want and which could be avoided easily by using the right tools for this task. And I think you should at least mention that. – Quasdunk Nov 27 '16 at 07:44
  • I think you are right. I will add a warning that `str_replace` will strip out every `)` @Quasdunk – Aniket Sahrawat Nov 27 '16 at 07:46
1

This looks a little JSONP-ish, so it actually should be valid JSON, except that it's wrapped within a funtion call. But that comma at the end of "domain": "http://www.google.com/", sure does not belong there.

You have several ways to go about it:

  1. strip the function wrapper

This only works if you know the function name. So if you know that it'll always be wrapped with WebInfo(...), you could just extract the JSON-part substring:

$jsonPart = substr($jsonpString, 8, -1); 

where 8 is the length of "WebInfo(" and "-1" takes care of the ")" at the end.

  1. Parse the JSON-part

Simply parse everything between the first "{" and the last "}":

preg_match("/\{(.*)\}/s", $jsonpString, $matches);

Since there should only be one match, you grab the first:

$jsonPart = $matches[0];

With this approach you don't need to know the wrapper-function name's name/length. But still you need to take care of that trailing comma mentioned above. Is it really there or was it just a copy-paste mistake?

Quasdunk
  • 14,944
  • 3
  • 36
  • 45
  • 1
    what is the difference between the 2 answers? – Teddy Codes Nov 27 '16 at 06:55
  • They would both work. you are essentially replacing the values with nothing. Looks the same to me. I haven't tested either solution, but they do the same thing. – Teddy Codes Nov 27 '16 at 07:07
  • @McStuffins But they don't. `str_replace` will replace *everything* including the actual payload. It may work in many, maybe most cases, but it's strictly tied to some assumptions that can easily change without your influence. It is simply not a good / robust approach. What if tha name is "Google ( my favorite search engine )"? – Quasdunk Nov 27 '16 at 07:14