Convert ndjson to json for HTML table - javascript
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...
Related
How to insert alid JSON that contains single quotes into MySQL
My goal is simply to convert a JavaScript object to JSON and insert it into a MySQL row that stores the MySQL JSON data type. This process must happen as part of a large batch insert, so I cannot edit the data manually. When call JSON.stringify on the object, I get valid JSON, but, because the object contains song lyrics that often contain single quotes, when I try to run the SQL query, MySQL throws a parse error. This works fine const validJson = '{"foo": "bar", "buzz": 1}'; INSERT INTO table_name ( songid, json ) VALUES ( ${song.songid}, ${validJson} ); But, this doesn’t const validJsonWithASingleQuote = {"foo's": "bar", "buzz": 1}'; INSERT INTO table_name ( songid, json ) VALUES ( ${song.songid}, ${validJsonWithASingleQuote} ); I also tried using a prepared statement with no luck PREPARE myInsert FROM 'INSERT INTO table_name ( songid, json ) VALUES ( ?, ? )'; SET #a = ${song.songid}; SET #b = ${JSON.stringify(r)}; EXECUTE myInsert USING #a, #b; I should also mention, that the original JavaScript objects contain strings that have single quotes escaped with "\". The JSON.stringify method, decodes those backslashes. Unfortunately, all of the SO questions I have found on this topic either recommend escaping single quotes with "\" manually or they have gone unresolved. Is there a programatic way to accomplish this? Perhaps a JavaScript or MySQL method that would generate valid JSON and leave the "\'" sequences in?
I finally found my way to this answer: https://stackoverflow.com/a/49525488/1359529 Turns out the Node.js driver for mysql contains an escape method. So, something like this works: PREPARE myInsert FROM 'INSERT INTO table_name ( songid, json ) VALUES ( ?, ? )'; SET #a = ${song.songid}; SET #b = ${mysql.escape(JSON.stringify(sr))}; EXECUTE myInsert USING #a, #b;
This tripped me up for a couple of days! I'm using the package and was having trouble with a single quote as a prop value: { name: "Jade's Palace", } I struggled to escape the single quote for mysql and could not create "Jade's Palace" because JS uses \ as its escape char. The solution was a prepared statement with the escape method. const query = 'INSERT INTO Places(id, data) VALUES ?'; const params = results.data?.map((data: any) => [ data.id, { toSqlString: () => connection?.escape(JSON.stringify(data)) }, ]); await connection.query(query, [params]);
Converting array inside a string to an array
I have a response which contains an array which is present inside string(""). What I need I just the array. How do I get rid of the quotes? I tried JSON.parse(). It gave me error message SyntaxError: Unexpected token ' in JSON at position 1 What I have is as follows: email_id: "['abc#mail.com', 'cde#mail.com']" What I need is as follows: email_id: ['abc#mail.com', 'cde#mail.com'] This is a key which a part of a large response I am getting from backend in my angular 7 app.
Below solution will work for your case provided the strings does not contain /' var a = "['abc#mail.com', 'cde#mail.com', 'def#mail.com']"; a = a.replace(/'/g, '"'); var result = JSON.parse(a); Considering its an email data, there's no possibility of having that escape character sequence
You have to send in backend the format with double quotes instead of one quotes or in front just replace quotes ' with double quotes " inside your array , otherwise the parse will fail , see below snippet : let json = { email_id: "['abc#mail.com', 'cde#mail.com']" } json.email_id = json.email_id.replace(/'/g,"\""); console.log(JSON.parse(json.email_id));
convert url encoded data to JSON array or js array
How can I convert data that looks like it is url encoded to a JSON array or js array. &nfl_s_delay=120&nfl_s_stamp=1109033755&nfl_s_left1=Cleveland%2024%20%20%20Cincinnati%203%20(END%20OF%204TH)&nfl_s_right1_count=0&nfl_s_url1=http://sports.espn.go.com/nfl/boxscore?gameId=400554328&nfl_s_left2=Kansas%20City%2017%20%20%20Buffalo%2013%20(00:00%20IN%204TH)&nfl_s_right2_count=0&nfl_s_url2=http://sports.espn.go.com/nfl/boxscore?gameId=400554332&nfl_s_left3=Miami%2016%20%20%20Detroit%2020%20(00:00%20IN%204TH)&nfl_s_right3_count=0&nfl_s_url3=http://sports.espn.go.com/nfl/boxscore?gameId=400554355&nfl_s_left4=^Dallas%2031%20%20%20Jacksonville%2017%20(FINAL)&nfl_s_right4_count=0&nfl_s_url4=http://sports.espn.go.com/nfl/boxscore?gameId=400554358&nfl_s_left5=San%20Francisco%2027%20%20%20New%20Orleans%2024%20(END%20OF%201ST%20OT)&nfl_s_right5_count=0&nfl_s_url5=http://sports.espn.go.com/nfl/boxscore?gameId=400554362&nfl_s_left6=Tennessee%207%20%20%20Baltimore%2021%20(00:00%20IN%204TH)&nfl_s_right6_count=0&nfl_s_url6=http://sports.espn.go.com/nfl/boxscore?gameId=400554367&nfl_s_left7=Pittsburgh%2013%20%20%20NY%20Jets%2020%20(END%20OF%204TH)&nfl_s_right7_count=0&nfl_s_url7=http://sports.espn.go.com/nfl/boxscore?gameId=400554370&nfl_s_left8=Atlanta%2027%20%20%20Tampa%20Bay%2017%20(END%20OF%204TH)&nfl_s_right8_count=0&nfl_s_url8=http://sports.espn.go.com/nfl/boxscore?gameId=400554372&nfl_s_left9=Denver%2041%20%20%20Oakland%2010%20(00:00%20IN%203RD)&nfl_s_right9_count=0&nfl_s_url9=http://sports.espn.go.com/nfl/boxscore?gameId=400554396&nfl_s_left10=St.%20Louis%2014%20%20%20Arizona%2010%20(00:00%20IN%202ND)&nfl_s_right10_count=0&nfl_s_url10=http://sports.espn.go.com/nfl/boxscore?gameId=400554397&nfl_s_left11=NY%20Giants%2017%20%20%20Seattle%2017%20(00:00%20IN%203RD)&nfl_s_right11_count=0&nfl_s_url11=http://sports.espn.go.com/nfl/boxscore?gameId=400554400&nfl_s_left12=Chicago%20at%20Green%20Bay%20(8:30%20PM%20ET)&nfl_s_right12_count=0&nfl_s_url12=http://sports.espn.go.com/nfl/preview?gameId=400554403&nfl_s_left13=Carolina%20at%20Philadelphia%20(8:30%20PM%20ET)&nfl_s_right13_count=0&nfl_s_url13=http://sports.espn.go.com/nfl/preview?gameId=400554408&nfl_s_count=13&nfl_s_loaded=true I would split split by &nfl_s_left2= , but the parameter increases by one for each time. So I could split by &nfl_s_left2= but that would only split once as the next thing that is similar is &nfl_s_left3= . So I guess a more specific question is can I split by a variable string? I could split by &nfl_s_left but then I'd get the number= in the value of the array. What would be the best way I could clean up this data?
how about this function url_split(str){ var jsArry={},tmp = str.split('&nfl_s_'),tmp2; tmp.forEach(function(e){ tmp2 = e.split('='); jsArry[tmp2[0]] = tmp2[1]; }); return jsArry; } and the fiddle demo http://jsfiddle.net/oswspkL0/
append text to a textarea with line breaks
I want to add some strings to a textarea which are file basenames. Everything is fine, but the only problem is that it mixes all the values and there are not any line breaks: var file_name = file.file_name; var base = new String(file_name).substring(file_name.lastIndexOf('/') + 1); if(base.lastIndexOf(".") != -1) base = base.substring(0, base.lastIndexOf(".")); $('textarea#image_Basename').append(base).split('\n'); These are my file basenames: 5b0cd65710052633dc5dcac406a382c4 212asaddgcvjh622sdsds22113554dfd 5sd5weea55rr6qasfdjkloijhj665s6a But after storing the data in to the database and retrieving it, the result I get is: 5b0cd65710052633dc5dcac406a382c4212asaddgcvjh622sdsds22113554dfd5sd5weea55rr6qasfdjkloijhj665s6a
To preserve newlines that are coming from a database or whatever, replace the newline characters with the HTML entity for a line feed: base = base.replace("\n", ' '); $('#image_Basename').append(base); If you're trying to append each string with a newline at the end, just concatenate it onto the string: $('#image_Basename').append(base + ' '); Also, you're using split on the textarea jQuery element, which doesn't make sense as it is an object not a string.
My Special thanks to #rink.attendant.6, his second method worked for me :) The answer is: $('#image_Basename').append(base + ' '); After adding this, I got all the file basenames in separate lines!
Convert array formatted strings to object
I got [[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27] this from google translator However I tried JSON.parse(xhr.responseText); It return an error Unexpected token
The problem is that this string contains multiple commas making your json invalid. You could try to replace it for a single one before parsing var x = '[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]' .replace(/,{2,}/g, ",") // 2 or more replace for 1 JSON.parse(x); Or if you have access to whatever is sending this string fix the output.
First you should remove an extra [] brackets by replacing that. ex, [["汽車","car","Qìchē",""]] should be: ["汽車","car","Qìchē",""] EDIT: you can refer to this answer: Parse Google Translate Json C# You should try: var str = '[[["汽車","car","Qìchē",""]],[["名詞",["汽車","車","轎車","車輛","車廂"],[["汽車",["car","automobile","auto"],,0.26497361],["車",["car","vehicle","lathe","machine","rook","turn"],,0.21967085],["轎車",["car","bus"],,0.020115795],["車輛",["vehicle","car"],,0.013611027],["車廂",["car"],,0.0042828997]]]],"en",,[["汽車",[4],0,0,1000,0,1,0]],[["car",4,[["汽車",1000,0,0],["車",0,0,0],["轎車",0,0,0],["車輛",0,0,0],["車廂",0,0,0]],[[0,3]],"car"]],,,[["en"]],27]'; var objstr = $.parseJSON(str);