0

consider the following url

index.php?json={uid:guest|10001441}

I am not able to decode the data . I need the output as {"uid":"guest|10001441"}.

The code i m using is

if (isset($_GET['json']))
{
   $url_data = $_GET['json'];
   $decoded_data = json_decode($url_data,true);
   var_dump($decoded_data);
}

But it gives me output as NULL. What am i doing wrong?? Do i need to pass data in a different format??

8
  • 1
    what does var_dump($_GET['json']) produce? Commented Jul 30, 2013 at 12:10
  • string(18) "{uid:guest|10001441}" Commented Jul 30, 2013 at 12:13
  • @Mayur You are getting wrong json data its not a valid json string . check where it is being generated and correct them. Commented Jul 30, 2013 at 12:14
  • What keeps you from using $_POST? Commented Jul 30, 2013 at 12:15
  • in the url the json is invalid, u forgot the quotes... better if you send by post no ? Commented Jul 30, 2013 at 12:15

5 Answers 5

2

It seems impossible to decode a JSON string without double quotes, as it's hard to determine whether a JSON string "{A:B:C}" is encoded from {"A":"B:C"} or from {"A:B":"C"}

You can use urlencode() to encode the JSON string or just ignore the extreme cases and add the double quotes manually to make the JSON string valid, hope this help :D

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

Comments

1

The data you're passing is not valid JSON. It doesn't have double quotes around it currently. Try this:

if (isset($_GET['json']))
{
   $url_data = '"'.$_GET['json'].'"';
   $decoded_data = json_decode($url_data,true);
   var_dump($decoded_data);
}

Output:

string(20) "{uid:guest|10001441}"

Also, I'd suggest using POST instead of GET here.

Comments

1

If you pass it like this it will work: ?json={"uid":"guest|10001441"}

But I am not sure whether it is a proper method.

4 Comments

are quotes allowed in url..i dnt think so
It works for me, you can use url_encode / decode , Still I am not sure it is a proper method
It works for me, you can use url_encode / decode , Still I am not sure it is a proper method
I just used your code and in url I added the quotes as in my answer thats it and worked
0

The JSON you have in your URL is not valid. PHP's json_decode looks for double quotes around the uid and it's value.

EDIT: Rather than storing the data in the URL as json could you not store the data as key value pairs such as

?uid=value

And then you could do something like

$data = json_encode( $_GET );

Comments

0

you can encode url parameter with base64_encode()

$json=base64_encode('{"uid":"guest|10001441"}');

url will look like this

index.php?json=eyJ1aWQiOiJndWVzdHwxMDAwMTQ0MSJ9

then do like this :-

if (isset($_GET['json']))
{
   $url_data = base64_decode($_GET['json']);
   $decoded_data = json_decode($url_data,true);
   var_dump($decoded_data);
}

4 Comments

That won't help if the original data isn't valid JSON.
@Barmar thanks to remind ,but i said it in comments that use a valid json string.
OK. But if you're going to encode a URL parameter, urlencode() is the usual format, so that it's still somewhat readable.
@Barmar if we use urlencode will be somting like this %7B%22uid%22%3A%22guest%7C10001441%22%7D, means to say use encoding .I would like it to send via post.

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.