1

New to angular, i'm trying to do a simple thing! I'd like to convert a json to a specific object. Still doesn't work at this time.

The JSON is acquired from an url parameter "token" encoded in base64.

Here is the code:

this.decodedToken: ViewToken = JSON.parse(JSON.stringify(this.DecodeBase64(this.token)));

console.log('token exists: ' + this.token);
console.log('token decoded: ' + this.decodedToken);            
console.log('token target: ' + this.decodedToken.target);
console.log('token access: ' + this.decodedToken.accessToken);

The target Object:

export class ViewToken {

    target: string;
    accessToken: string;

}

And here is the result:

token exists: ew0KICAgIHRhcmdldDogIm15VGFyZ2V0IiwNCiAgICBhY2Nlc3NUb2tlbjogIlRHOXlaVzBnYVhCemRXMGdaRzlzYjNJZ2MybDBJR0Z0WlhRc0lHTnZibk5sWTNSbGRIVnlJR0ZrYVhCcGMyTnBibWNnWld4cGRBPT0iDQp9 
token decoded: {
    target: "myTarget",
    accessToken: "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdA=="
}
token target: undefined
token access: undefined

I tried many solutions but none of them worked! I still can't map the JSON to my object.

Solutions I tried so far:

  • json2typescript package -> gives me error on the deserialize function
  • fromJson method -> gives me a table of each character in the json

    static fromJson(json: any) {
    
        let obj: IViewToken = Object.create(ViewToken.prototype);
    
        return Object.assign(obj, json, {
            target: json.target,
            accessToken: json.accessToken
        });
    
    }
    
  • object mapping key,value to object -> doesn't work since the json object attributes are undefined

EDIT:

Correction to make it work: I don't have to JSON.stringify my decoded base64 token

this.decodedToken = JSON.parse(this.DecodeBase64(this.token));
Dams
  • 141
  • 3
  • 15
  • 1
    Your token isn't valid JSON, the keys aren't quoted. See the second answer on the duplicate: https://stackoverflow.com/a/13718664/3001761. – jonrsharpe Jun 01 '18 at 08:43
  • 1
    Possible duplicate of [javascript string to object](https://stackoverflow.com/questions/13718326/javascript-string-to-object) – jonrsharpe Jun 01 '18 at 08:45
  • quoted keys or not, I have the same result :/ – Dams Jun 01 '18 at 08:47
  • I doubt it, `JSON.parse('{"target": "myTarget", "accessToken": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdA=="}')` works fine for me. – jonrsharpe Jun 01 '18 at 08:48
  • If I directly inject the json in the JSON.parse, it works indeed. But if I decode the json from the base64 encoded token, it dosn't! even with the quotes. I guess there is something I missing – Dams Jun 01 '18 at 08:54
  • 1
    What do you mean *"even with the quotes"*? If you're trying `this.decodedToken['target']` then of course that also doesn't work, it's not making any difference. The keys aren't quoted correctly **in the token**, so it can't be `JSON.parse`d properly. And when you stringify *then* parse it you end up right back where you started, that's a no-op. – jonrsharpe Jun 01 '18 at 08:56
  • Ok so this is indeed the JSON.stringify that gives me wrong results! Thakns for the help! – Dams Jun 01 '18 at 09:13

0 Answers0