Parse json without changing the object structure

I’m new to js and i want to parse a JSON into an instance of this class:

class Client {
    name_ =null;  
    age_ =null
    something_=null ;

   get Name()
   {
       return _name ; 
   }
   set Name(val) {
       _name = val ; 

   }
   get Age()
   {
       return _age ; 
   }
   set Age(val) {
       _age = val ; 

   }

   get Something()
   {
       return something_ ; 
   }
   set Something(val) {
    something_ = val ; 

   }
}

i tried this :

let client1 = new Client() ;
let js =`
  {
    "Name": "Estes Benjamin",
    "Age": 31
  }
`; 

client1 = JSON.parse(js);
console.log(client1); 

this code give me this result :

Object { Name: "Estes Benjamin", Age: 31 }

but what i’m looking for is this (the original object populated with the JSON content )

Object { Name: "Estes Benjamin", Age: 31, Something: null }

Answer

client1 = JSON.parse(js) completely replaces the value that was in client1, so the Client instance is no longer there.

You can update the properties on that instance instead via Object.assign:

let client1 = new Client();
let json =`
  {
    "Name": "Estes Benjamin",
    "Age": 31
  }
`; 

Object.assign(client1, JSON.parse(json));
console.log(client1);

Live Example (with the updated Client code shown below):

class Client {
    _name = null;  
    _age = null;
    _something = null;

    get Name() {
       return this._name; 
    }

    set Name(val) {
       this._name = val; 
    }

    get Age() {
       return this._age; 
    }

    set Age(val) {
       this._age = val; 
    }

    get Something() {
       return this._something; 
    }

    set Something(val) {
        this._something = val;
    }
}

let client1 = new Client();
let json =`
  {
    "Name": "Estes Benjamin",
    "Age": 31
  }
`; 

Object.assign(client1, JSON.parse(json));
console.log(client1);

That takes all of the properties from the newly-created object from JSON.parse and copies them into the Client instance in client1.

Also note that as Ivar said, there are some issues with Client. The main two are:

  1. You’re sometimes using _ after the property name, other times using it before.
  2. In JavaScript, to access a property on the instance, you have to explicitly reference the instance. In a method, you typically do that with the this keyword.

Here’s a version with those fixed:

class Client {
    _name = null;  
    _age = null;
    _something = null;

    get Name() {
       return this._name; 
    }

    set Name(val) {
       this._name = val; 
    }

    get Age() {
       return this._age; 
    }

    set Age(val) {
       this._age = val; 
    }

    get Something() {
       return this._something; 
    }

    set Something(val) {
        this._something = val;
    }
}

Beyond that, though, some observations (mostly just FYI):

  • Properties prefixed with _ are still public and can still be accessed directly. (For instance, client1._age would give us 31 in the example above.) (JavaScript does have actual private fields now, though they’re very new.)
  • There’s not a lot of point to those accessor properties vs. just using a data property as the class stands now. But if you were adding side-effects or logging or something, or only providing a getter but no setter, those are (or can be) use cases for accessor properties.
  • Even accessor properties in JavaScript are generally written with a lower case initial letter.
  • Your code relies on the relatively-new class fields proposal (but it’s broadly-supported other than in obsolete browsers like Internet Explorer 11). You’re using that feature where you define your instance properties (_name, etc.). Prior to the class fields stuff being added, you had to do that inside the constructor as an assignment (this._name = null;).

Just FWIW, if you wanted to use private fields (which, again, are very new):

class Client {
    #name = null;
    // ...
    get name() {
        return this.#name;
    }
    set name(value) {
        this.#name = value;
    }
    // ...
}

With that, client1.#age is an error. You can’t access it outside the class.