简体   繁体   中英

Convert ndjson to json for HTML table

I would like to know if it is possible to convert the ndjson data from this API: https://lichess.org/api/team/overberg-chess-group/users and turn it into an HTML table. I have found some javascript snippets that will convert normal json into an html table but not ndjson. Any help would be appreciated

Looks like the API pulls a file that's very close to JSON. Assuming that the API data is in var sourceData , just a few tweaks are required...

// Add commas between the list of objects...
data = sourceData.split( '}\n{' ).join( '},{' );

// ...remove all carriage return line feeds...
data = data.split( '\r\n' ).join( '|' );

// ...and there's one instance of a double quoted string, 
// eg, "bio":""I am committed..."".  This appears to be
// the only occurrence of this anomaly, so the following
// currently works, but you might run into issues in the
// future, for example, if an attribute is a null string "".
data = data.split( '""' ).join( '"' );

let dataObject = JSON.parse( '[' + data + ']' );

At this point, dataObject contains the object data representing the ndjson data pulled via the API. Note that when you go to place this object into a HTML table, you'll need to convert the pipe characters "|"back into line feeds... This should help you along your way...

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