2

I am using ngx-cookie-service package

{
    "user": [
        {
            "id": 109,
            "name": "name1",
            "job" : "job name"

        }
    ],
    "token":"xxxx"
}

    this.cookieService.set( 'user_id', result.user.id );
    this.cookieService.set( 'user_name', result.user.name );
    this.cookieService.set( 'user_job', result.user.job );

How can I save this as a JSON array instead of saving individual?

like this.cookieService.set( 'user', result.user );

Pavan Jadda
  • 4,306
  • 9
  • 47
  • 79
ShibinRagh
  • 6,530
  • 4
  • 35
  • 57

2 Answers2

14

You can use stringify() and save it and then parse() to get the value back

To set the value:

this.cookieService.set('user', JSON.stringify(result));

To retrieve the value:

JSON.parse(this.cookieService.get('user'));
Christie
  • 829
  • 8
  • 16
2

Here is example for adding Object in Cookie

this.cookieService.set( 'user', JSON.stringify(result));

Sameer
  • 131
  • 1
  • 9