1

Is there a way to parse a JSON string into an existing Javascript object: Lets say i have created this object:

var ClientState = function(){
    this.userId ="";
    this.telephoneState = "UNKNOWN";
    this.agentState = "UNKNOWN";
    this.muteState = "UNKNOWN";
    this.number = "";
    this.ready = false;
}

ClientState.prototype = {
    doNastyStuff: function(){
        //do something here
    }
    //other methods here
}

I have this json coming through the wire:

{"userId":"xyz","telephoneState":"READY","agentState":"UNKNOWN","muteState":"MUTED","number":"","ready":false}

Is it possible to deserialize into the object specified above? So that i can use all methods specified on it? Or in general is it possible to deserialize into a specific target object (without specifying deserialization in this target object)? (I know that i could create an constructor that accepts json or a parsed object.)

3 Answers 3

2

Yes! You can use Object.assign to overwrite the attributes of an object with another object:

var ClientState = function() {
  this.userId = "";
  this.telephoneState = "UNKNOWN";
  this.agentState = "UNKNOWN";
  this.muteState = "UNKNOWN";
  this.number = "";
  this.ready = false;
}

var c = new ClientState();
console.log('prior assignment: ', c);

Object.assign(c, {
  "userId": "xyz",
  "telephoneState": "READY",
  "agentState": "UNKNOWN",
  "muteState": "MUTED",
  "number": "",
  "ready": false
});

console.log('after assignment: ', c);

Note that it will overwrite all the properties of source object (first object) with the target object (second object) by matching the respective keys. The keys, which are non-existing in the target object are left intact in the source object.

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

1 Comment

this is exactly what i need :-). Can i assign the json string directly or do i have to "parse" it first?
0

Is this what you had in mind?

function parseAs(targetClass, rawJSON) {
    return Object.assign(new targetClass(), JSON.parse(rawJSON));
}

var clientState = parseAs(ClientState, '{"userId":"xyz"}');

I don't think there's a native JSON method that does this, but you can just write a function like the one above. You can even define it on the JSON class itself:

JSON.parseAs = function(targetClass, rawJSON) {
    return Object.assign(new targetClass(), JSON.parse(rawJSON));
}

Comments

0

If you want to use your function to add the object values to the instance properties:

function ClientState() {
  this.userId = "";
  this.telephoneState = "UNKNOWN";
  this.agentState = "UNKNOWN";
  this.muteState = "UNKNOWN";
  this.number = "";
  this.ready = false;
}

ClientState.prototype = {
  doNastyStuff: function(json) {
    const data = JSON.parse(json);
    Object.assign(this, data);
  }
}

const json = '{"userId":"xyz","telephoneState":"READY","agentState":"UNKNOWN","muteState":"MUTED","number":"","ready":false}';
const clientState = new ClientState();
clientState.doNastyStuff(json);
console.log(clientState);

Comments

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.