How to convert JSON to CSV format and store in a variable

前端 未结 16 2313
一整个雨季
一整个雨季 2020-11-22 17:31

I have a link that opens up JSON data in the browser, but unfortunately I have no clue how to read it. Is there a way to convert this data using JavaScript in CSV format and

16条回答
  •  萌比男神i
    2020-11-22 17:38

    Personally I would use d3-dsv library to do this. Why to reinvent the wheel?

    
    import { csvFormat } from 'd3-dsv';
    /**
     * Based on input data convert it to csv formatted string
     * @param (Array) columnsToBeIncluded array of column names (strings)
     *                which needs to be included in the formated csv
     * @param {Array} input array of object which need to be transformed to string
     */
    export function convertDataToCSVFormatString(input, columnsToBeIncluded = []) {
      if (columnsToBeIncluded.length === 0) {
        return csvFormat(input);
      }
      return csvFormat(input, columnsToBeIncluded);
    }
    

    With tree-shaking you can just import that particular function from d3-dsv library

提交回复
热议问题