How clean the javascript object by replace() in javascript

70 Views Asked by At

I have a object in this format

{"paymethod_id":1,"business_id":76,"delivery_type":"1","driver_tip":0,"delivery_zone_id":6569,"delivery_datetime":null,"location":{"lat":18.7675049,"lng":-103.1445221},"deliveryOptionmodal":{"id":3,"value":"Sin contacto/Dejar orden en la puerta","$$hashKey":"object:272"},"delivery_cost_new":10,"products":{"name":"Product"},"customer_id":35,"customer":"{\"id\":35,\"name\":\"Hong Kong\",\"middle_name\":null,\"lastname\":\"\",\"second_lastname\":null,\"photo\":\"https://res.cloudinary.com/ordering2/image/upload/v1551225299/taomauvuhrrowqqp3ncp.png\",\"email\":\"[email protected]\",\"cellphone\":\"4433413248\",\"address\":\"Coalcomán, Mich., México\",\"location\":\"{\\\"lat\\\":18.7675049,\\\"lng\\\":-103.1445221}\",\"internal_number\":null,\"address_notes\":null,\"zipcode\":null,\"map_data\":{\"library\":\"google\",\"place_id\":\"ChIJz6WGrUw-MIQR_jYIoFZ-RPM\"},\"tag\":\"home\"}","business_name":"Soporte Devy"}

Which is not easily readable is there any way i can clean this object and see like this

business_name: Sport Devy
name: hong kong

I just want to clean the object and convert it into representable form

2

There are 2 best solutions below

0
Goblinlord On BEST ANSWER

You could write a function to transform it into the format you want. I am not sure if you specifically want it as a string or as a different object. I am outputting a string but you could modify this to return an object if that is what you need.

function formatDisplay(obj) {
   const bname = obj.business_name;
   const customer = JSON.parse(obj.customer || "{}");
   const name = customer && customer.name;
   /* If you need object:
   return {
     business_name: bname,
     name: name
   };
   */
   return [
    "business name: "+ bname,
    "name: " + name,
   ].join("\n");
};

const data = {"paymethod_id":1,"business_id":76,"delivery_type":"1","driver_tip":0,"delivery_zone_id":6569,"delivery_datetime":null,"location":{"lat":18.7675049,"lng":-103.1445221},"deliveryOptionmodal":{"id":3,"value":"Sin contacto/Dejar orden en la puerta","$$hashKey":"object:272"},"delivery_cost_new":10,"products":{"name":"Product"},"customer_id":35,"customer":"{\"id\":35,\"name\":\"Hong Kong\",\"middle_name\":null,\"lastname\":\"\",\"second_lastname\":null,\"photo\":\"https://res.cloudinary.com/ordering2/image/upload/v1551225299/taomauvuhrrowqqp3ncp.png\",\"email\":\"[email protected]\",\"cellphone\":\"4433413248\",\"address\":\"Coalcomán, Mich., México\",\"location\":\"{\\\"lat\\\":18.7675049,\\\"lng\\\":-103.1445221}\",\"internal_number\":null,\"address_notes\":null,\"zipcode\":null,\"map_data\":{\"library\":\"google\",\"place_id\":\"ChIJz6WGrUw-MIQR_jYIoFZ-RPM\"},\"tag\":\"home\"}","business_name":"Soporte Devy"};

console.log(formatDisplay(data));

0
Diwakar Singh On
const obj = {
  "paymethod_id": 1,
  "business_id": 76,
  "delivery_type": "1",
  "driver_tip": 0,
  "delivery_zone_id": 6569,
  "delivery_datetime": null,
  "location": {
    "lat": 18.7675049,
    "lng": -103.1445221
  },
  "deliveryOptionmodal": {
    "id": 3,
    "value": "Sin contacto/Dejar orden en la puerta",
    "$$hashKey": "object:272"
  },
  "delivery_cost_new": 10,
  "products": {
    "name": "Product"
  },
  "customer_id": 35,
  "customer": "{\"id\":35,\"name\":\"Hong Kong\",\"middle_name\":null,\"lastname\":\"\",\"second_lastname\":null,\"photo\":\"https://res.cloudinary.com/ordering2/image/upload/v1551225299/taomauvuhrrowqqp3ncp.png\",\"email\":\"[email protected]\",\"cellphone\":\"4433413248\",\"address\":\"Coalcomán, Mich., México\",\"location\":\"{\\\"lat\\\":18.7675049,\\\"lng\\\":-103.1445221}\",\"internal_number\":null,\"address_notes\":null,\"zipcode\":null,\"map_data\":{\"library\":\"google\",\"place_id\":\"ChIJz6WGrUw-MIQR_jYIoFZ-RPM\"},\"tag\":\"home\"}",
  "business_name": "Soporte Devy"
}

business_name can be read directly from the object - obj.business_name. customer node needs to parsed into javascript object; for that you can use JSON.parse(obj.customer)