简体   繁体   中英

Javascript parsing json object

As you can see based on the columns from the excel one column has "space" which is the Categorized Options that when being parsed the 'Categorized Options': was mixed to the Options key as you can see on the json data below. Categorized Options is the only column key that has space. The Categorized Options should be a seperate key. Is there a way we can alter the paparse json result below to seperate the Categorized Options: key and value to get the desired result ?

Desired Output - The ouput i wanna get

 [{ Type: 'New',   
  Stock: 'P10092',
  VIN: '1G1YY34U455128500',    
  Year: '2005',
  Options:
   ' Switch',
  Categorized Options:
  'LICENSE PLATE BRACKET',
   ImageList:
   'http: image.com',
  Comment: '2005 CHEVY CORVETT',
  FuelType: 'Gasoline Fuel',
  DriveType: 'RWD' }]

Excel columns

Type | VIN | Stock | Year | Options | Categorized Options | ImageList | Comment | FuelType | DriveType
New    | 1G..  | P10092   |  2005   |  Switch   | LICENSE PLATE BRACKET | http: image.com 2005  | comment test  | Gasoline Fuel | RWD

Using Paparse (The data result is now )

this is the result after paparse the 'Categorized Options': was being joined to the Options , the output i want is the data above i posted. Categorized Options should be a seperate key

[{ Type: 'New',   
  Stock: 'P10092',
  VIN: '1G1YY34U455128500',    
  Year: '2005',
  Options:
   ' Switch',
   'Categorized Options':
   'LICENSE PLATE BRACKET,',
   ImageList:
   'http: image.com',
  Comment: '2005 CHEVY CORVETT',
  FuelType: 'Gasoline Fuel',
  DriveType: 'RWD' }

Make sure to set the delimiter to | . You can also use transform and transformHeader to trim the values.

More informations on PapaParse docs .

 const dataText = document.getElementById('data').textContent.trim(); const parsed = Papa.parse(dataText, { header: true, delimiter: '|', transformHeader: header => header.trim(), transform: value => value.trim() }); document.querySelector('#output').innerHTML = JSON.stringify(parsed, null, 2); 
 <script src="https://www.papaparse.com/resources/js/papaparse.js"></script> <h3>Data</h3> <pre id="data"> Type | VIN | Stock | Year | Options | Categorized Options | ImageList | Comment | FuelType | DriveType New | 1G.. | P10092 | 2005 | Switch | LICENSE PLATE BRACKET | http: image.com 2005 | comment test | Gasoline Fuel | RWD </pre> <h3>Output</h3> <pre id="output"> </pre> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM