0

i have a method in my service to save a date in my server, when this method sends the date to the server, it sends same fields as null in the json, how can i remove the fields with null value?

public create<T>(post: any): Observable<T> {
    const httpOptions = {
        headers: new HttpHeaders({
            'Content-Type': 'application/json'
        })
    };
    return this.httpClient
        .post<T>(`${this.url}`, JSON.stringify(post), httpOptions)
        .pipe(catchError((err, source) => this.responseHandler.onCatch(err, source)));
}

the json send to server:

{
   "name": "test",
   "professionType":{
      "id": null
   },
   "comment": null,
   "organizationSpecialities":[
        {
         "speciality":{
            "id": null
         },
         "effective": 2,
         "effectiveDate":{
            "startDate": null,
            "endDate": "2019/12/01"
         }
      }
   ]
}

the json i want to send:

{
   "name": "test",
   "organizationSpecialities":[
         "effective": 2,
         "effectiveDate":{
            "endDate": "2019/12/01"
         }
      }
   ]
}
1
  • this will be pretty much to write, i think you need to go trought the Object.keys() recursively (when nested), and delete property that equals null Commented Dec 10, 2018 at 16:35

1 Answer 1

11

You can loop through the JSON and remove if the value is null or Object.keys(o).length === 0.

Following is the code.

cleanData(o) {
  if (Object.prototype.toString.call(o) == "[object Array]") {
    for (let key = 0; key < o.length; key++) {
      this.cleanData(o[key]);
      if(Object.prototype.toString.call(o[key]) == "[object Object]") {
        if(Object.keys(o[key]).length === 0){
          o.splice(key, 1);
          key--;
        }
      }

    }
  }
  else if (Object.prototype.toString.call(o) == "[object Object]") {
    for (let key in o) {
      let value = this.cleanData(o[key]);
      if (value === null) {
        delete o[key];
      }
      if(Object.prototype.toString.call(o[key]) == "[object Object]") {
        if(Object.keys(o[key]).length === 0){
          delete o[key];
        }
      }
      if(Object.prototype.toString.call(o[key]) == "[object Array]") {
        if(o[key].length === 0){
          delete o[key];
        }
      }
    }
  }
  return o;
}

For reference added stackblitz code.

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

3 Comments

Awesome answer, I deleted my answer and up voted this one.
when i put all the values that are in the list ' organizationSpecialities' to null, json looks like this:"organizationSpecialities": [ {} ], but in fact it should delete the the list
@AymenKanzari Updated the code in stackblitz.com/edit/… and also in the answer.

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.