-3

What will be the best way to simplify this json? My function returns a json like this:

   {
    "token": {
        "0": "MJfdZLQRsu42VmUFzc9jozCa6mtJ0KJwziBEv3IXfr9RW_uhws",
        "user_id": 123,
        "username": "name"
    }
}

My goal is to achieve this:

{
   'token': "MJfdZLQRsu42VmUFzc9jozCa6mtJ0KJwziBEv3IXfr9RW_uhws",
   'username: "name",
   'user_id': '168'
}

This is the method that returns the json:

class MyJWTManager extends JWTManager
{
    public function create(UserInterface $user)
    {
        $payload = ['roles' => null];
        $this->addUserIdentityToPayload($user, $payload);

        $jwtCreatedEvent = new JWTCreatedEvent($payload, $user);
        $this->dispatcher->dispatch(Events::JWT_CREATED, $jwtCreatedEvent);

        $jwtString = $this->jwtEncoder->encode($jwtCreatedEvent->getData());

        $jwtEncodedEvent = new JWTEncodedEvent($jwtString);
        $this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent);

        return [
            $jwtString,
            'user_id' => $user->getId(),
            'username' => $user->getUsername()
        ];
        // return $jwtString // outputs "token": "ssdsmdmasdms;dm;samd;msdm;sdm;sdm"    
        //return $userInfo;

    }
}
Capfer
  • 829
  • 9
  • 22
  • Possible duplicate of [Flattening a JSON multi depty array in PHP](https://stackoverflow.com/questions/28393612/flattening-a-json-multi-depty-array-in-php) – treyBake May 31 '19 at 13:56
  • 1
    How is the function generating the JSON. Code example please. – mlewis54 May 31 '19 at 14:04
  • mlewis54, thanks for showing interest. I generating a token using Symfony JWTAuthenticationBundle. Initially, the function returned just a string "token" like this: "token": "MJfdZLQRsu42VmUFzc9jozCa6mtJ0KJwziBEv3IXfr9RW_uhws". But, now I'm trying to add the username and the id. Here is the code, please: https://pastebin.com/dmwJ1TYB – Capfer May 31 '19 at 14:18
  • 1
    __Paste code here__. – u_mulder May 31 '19 at 14:26
  • Without getting too deeply into it, it may be as simple as instead of returning $jwtString in the return statement, you return: "token"=>$jwtString['token'] in its place. Please understand that I know nothing about the return from the Symphony function. It appears to be returning an array. – mlewis54 May 31 '19 at 14:32
  • u_mulder, I have edited my question above, please check. Thanks in advance – Capfer May 31 '19 at 14:38
  • mlewis54, $jwtString is a string. Therefore, I can't access it as an array – Capfer May 31 '19 at 14:43
  • Then you need to make it into a key value pair for the array, you want to have an entry in your return array that is 'token' => $value_returned from function. Could you provide the exact return string from the function please? – mlewis54 May 31 '19 at 14:54
  • mlewis54, { "token": "Nlcm5hbWUiOiJHQmVydHNvcyIsImlhdC" } – Capfer May 31 '19 at 14:56
  • Okay since the result is coming back as a JSON string I would (for quick and dirty) do: $ra=json_encode($jwtString); and then replace $jwtString in the return with 'token' => $ra['token'], If you like that and it works I'll post it as an answer. – mlewis54 May 31 '19 at 15:00
  • mlewis54, It doesn't work. This is definitely not the way to go. – Capfer May 31 '19 at 15:54
  • 1
    @mlewis54 `json_decode` – Arleigh Hix Jun 01 '19 at 03:37
  • @ArleighHix You are correct. My typing auto-pilot was on. It should read json_decode($jwtString,1); Thanks for the catch. – mlewis54 Jun 01 '19 at 15:17

1 Answers1

0

Extract the token value and return it as part of the return array

class MyJWTManager extends JWTManager
{
    public function create(UserInterface $user)
    {
        $payload = ['roles' => null];
        $this->addUserIdentityToPayload($user, $payload);

        $jwtCreatedEvent = new JWTCreatedEvent($payload, $user);
        $this->dispatcher->dispatch(Events::JWT_CREATED, $jwtCreatedEvent);

        $jwtString = $this->jwtEncoder->encode($jwtCreatedEvent->getData());

        $jwtEncodedEvent = new JWTEncodedEvent($jwtString);
        $this->dispatcher->dispatch(Events::JWT_ENCODED, $jwtEncodedEvent);
        $ra=json_decode($jwtString,1);
        return [
            'token' => $ra['token'],
            'user_id' => $user->getId(),
            'username' => $user->getUsername()
        ];

    }
}
mlewis54
  • 2,372
  • 6
  • 36
  • 58
  • mlewis54, thanks a lot. But I'm still getting unexpected results. The way I solved is by doing as the developers of this bundle has suggested in here: https://github.com/lexik/LexikJWTAuthenticationBundle/blob/master/Resources/doc/7-manual-token-creation.md – Capfer Jun 03 '19 at 14:04