簡體   English   中英

如何從 json 創建 csv 文件

[英]How to create csv file from json

我有一個 json 之類的

 { "connectEnd": 1366.2749999930384, "connectStart": 175.91999999422114, "decodedBodySize": 3360, "domComplete": 10424.984999990556, "domContentLoadedEventEnd": 6581.454999992275, "domContentLoadedEventStart": 6581.454999992275, "domInteractive": 6581.420000002254, "domainLookupEnd": 175.91999999422114, "domainLookupStart": 12.000000002444722, "duration": 10425.015000000712, "encodedBodySize": 1279, "entryType": "navigation", "fetchStart": 0.22499999613501132, "initiatorType": "navigation", "loadEventEnd": 10425.015000000712, "loadEventStart": 10424.994999993942, "name": "https://something/login", "nextHopProtocol": "http/1.1", "redirectCount": 0, "redirectEnd": 0, "redirectStart": 0, "requestStart": 1366.394999990007, "responseEnd": 2062.7999999996973, "responseStart": 2059.7599999891827, "secureConnectionStart": 414.94000000238884, "serverTiming": [], "startTime": 0, "transferSize": 2679, "type": "navigate", "unloadEventEnd": 0, "unloadEventStart": 0, "workerStart": 0, "workerTiming": [] }

我使用 papaparse 將 JSON 轉換為 csv ,我得到了這個:

"請求時間","第一個字節的時間","響應時間","請求響應時間","緩存搜索加響應時間","Dom交互","Dom完成","傳輸大小","持續時間", "Domain lookup time taken","Connection time taken" 693.3649999991758,3.040000010514632,3.040000010514632,696.4050000096904,2062.5750000035623,6581.420000002254,10424.984999990556,2679,10425.015000000712,163.91999999177642,1190.3549999988172

我打算使用名為BenchMark Evaluator的 Jenkins 插件

此插件僅接受以下格式的 csv: csv 表格圖片鏈接

我的問題陳述:如何將已解析的 csv 的結構更改為所需的 csv 格式。 是否有 npm package 可以直接給我我想要的,或者解析后的 csv 需要轉換。

將 json 轉換為 csv 的更優雅的方法是在沒有框架的情況下使用 map ZC1C425268E68385D1AB5074C17:

var json = json3.items
var fields = Object.keys(json[0])
var replacer = function(key, value) { return value === null ? '' : value } 
var csv = json.map(function(row){
  return fields.map(function(fieldName){
    return JSON.stringify(row[fieldName], replacer)
  }).join(',')
})
csv.unshift(fields.join(',')) // add header column
 csv = csv.join('\r\n');
console.log(csv)

Output:

title,description,link,timestamp,image,embed,language,user,user_image,user_link,user_id,geo,source,favicon,type,domain,id
"Apple iPhone 4S Sale Cancelled in Beijing Amid Chaos (Design You Trust)","Advertise here with BSA Apple cancelled its scheduled sale of iPhone 4S in one of its stores in China’s capital Beijing on January 13. Crowds outside the store in the Sanlitun district were waiting on queues overnight. There were incidents of scuffle between shoppers and the store’s security staff when shoppers, hundreds of them, were told that the sales [...]Source : Design You TrustExplore : iPhone, iPhone 4, Phone","http://wik.io/info/US/309201303","1326439500","","","","","","","","","wikio","http://wikio.com/favicon.ico","blogs","wik.io","2388575404943858468"
"Apple to halt sales of iPhone 4S in China (Fame Dubai Blog)","SHANGHAI – Apple Inc said on Friday it will stop selling its latest iPhone in its retail stores in Beijing and Shanghai to ensure the safety of its customers and employees. Go to SourceSource : Fame Dubai BlogExplore : iPhone, iPhone 4, Phone","http://wik.io/info/US/309198933","1326439320","","","","","","","","","wikio","http://wikio.com/favicon.ico","blogs","wik.io","16209851193593872066"

使用這種不那么密集的語法以及 JSON.stringify 為字符串添加引號,同時保持數字不被引用:

const items = json3.items
const replacer = (key, value) => value === null ? '' : value // specify how you want to handle null values here
const header = Object.keys(items[0])
const csv = [
  header.join(','), // header row first
  ...items.map(row => header.map(fieldName => JSON.stringify(row[fieldName], replacer)).join(','))
].join('\r\n')

console.log(csv)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM