Convert csv output toobject with nested arrays and objects using javascript

Viewed 10

I have object after CSV conversion in this shape:

const data = {
  "assets/0/description": "propertyImage",
  "assets/0/filename": "a31989e07277954d93bca65e2d02dcb9.jpeg?p=cc_vo_small",
  "assets/0/mimetype": "jpeg?p=cc_vo_small",
  "assets/0/ordinal": "0",
  "assets/0/url":  "/usados/2022/09/04/0/a31989e07277954d93bca65e2d02dcb9.jpeg?p=cc_vo_small",
  "assets/1/description": "propertyImage",
  "assets/1/filename": "3bc3a9a4982508941ea178cca2ad31f1.jpeg?p=cc_vo_small",
  "assets/1/mimetype": "jpeg?p=cc_vo_small",
  "assets/1/ordinal": "1",
  "assets/1/url": "",

  combustion: "diesel",
  createdAt: "2022-09-14T13:42:55.807Z",
  engine: "235 CV",
  firstDateRegistration: "2021",
  gearbox: "automática secuencial",
  "location/city": "Vitoria-Gasteiz",
  "location/country": "España",
  "location/distributor": "",
  "location/houseNumber": "",
  "location/postalCode": "",
  "location/province": "Alava",
  "location/street": "C/ Portal de Betoño, 13-15",
  make: "volvo",
  "metadata/0/key": "Año",
  "metadata/0/value": "2021",
  mileage: "29000",
  model: "v90 cross country b5 pro awd aut.",
  "optionals/0": "",
  originalColorString: "gris",
  "price/currency": "EUR",
  "price/financialPrice": "53700",
  "price/price": "56999",
  saleDate: "Wed Sep 14 2022",
  seller_type: "professional",
  traction: "",
  version: "V90 Cross Country B5 Pro Awd Aut."
};

Im trying to convert nested items that csv made flat into the correct format:

assets/0/description -> { 
  assets: [
    { 
      description: "propertyImage",
      filename: "...",
      ...
    }
  ]
}

probably number between is an array and just / is an object. I tried to build reduce function and create it recursively but it looks more complicated than this. Any chance that there is a library for it? Or has someone already met this problem in the past?

0 Answers
Related