zemirco / json2csv

Convert json to csv with column titles
http://zemirco.github.io/json2csv
MIT License
2.72k stars 362 forks source link

Applying Data Transforms #535

Closed KhalidH82 closed 3 years ago

KhalidH82 commented 3 years ago

Hi, I'm looking to add some data transforms to the cell values in my CSV. I've created a function that receives the data item and returns the item.

const transformedItem = (data) => ({
  ...data,
  url: "https://www.londontheatre.co.uk/show/".concat(data.name),
  price_from_default: "from £".concat(data.price_from.default),
});`

My question is how do I apply this transformed data to the parser so that it will reflect in the CSV. Thanks in advance for your help. My code is below...

`const fs = require("fs");
const { Parser } = require("json2csv");
const data = require("./browse.json");
const fields = [
  {
    label: "ID",
    value: "native_id",
  },
  {
    label: "Final URL",
    value: "url",
  },
  {
    label: "Image URL",
    value: "image_url",
  },
  {
    label: "Item title",
    value: "name",
  },
  {
    label: "Item description",
    value: "context_label",
  },
  {
    label: "Formatted sale price",
    value: "price_from.default",
  },
];

const transformedItem = (data) => ({
  ...data,
  url: "https://www.londontheatre.co.uk/show/".concat(data.name),
  price_from_default: "from £".concat(data.price_from.default),
});
console.log(transformedItem(data[0]));

try {
  const parser = new Parser({ fields });
  const csv = parser.parse(data);
  fs.writeFile("products.csv", csv, "utf8", function (err) {
    if (err) {
      console.log(
        "Some error occured - file either not saved or corrupted file saved."
      );
    } else {
      console.log("It's saved!");
    }
  });
  // console.log(csv);
} catch (err) {
  console.error(err);
}
knownasilya commented 3 years ago

fields[].value can also be a function, see https://github.com/zemirco/json2csv/tree/v5#example-fields-option

KhalidH82 commented 3 years ago

Thanks for your quick response! I'm trying to access a field from an object. I've used dot notation and brackets but was unsuccessful. Does this library support this?

 {
    label: "Formatted sale price",
    value: (row) => "from £".concat(**_row.price_from.default_**),
  },

Thanks again 🙏

knownasilya commented 3 years ago

I'd have to see a sample of you data to answer that, but generally yes.

KhalidH82 commented 3 years ago

Here is a sample of the field I'm looking to access. "price_from": { "default": 20 },

I'm looking to concat "from £", but in the CSV it shows... "from £[object Object]"

This is the code used... { label: "Formatted sale price", value: (row) => "from £".concat(_row.pricefrom.default), },

Let me know if this helps.

knownasilya commented 3 years ago

Looks like the output is not what is expected I recommend console.log(row.price_from.default) to see what you get back. Seems like default is an object, not a string.

KhalidH82 commented 3 years ago

Looks like the output is not what is expected I recommend console.log(row.price_from.default) to see what you get back. Seems like default is an object, not a string.

Thanks for your help and support!