Search code examples
javascriptjsonpublic-suffix-list

Creating JSON object from text file values


I want to convert the plaintext Public Suffix List to JSON object so as to use it in my script. I just want the list of TLD's.
The list is here https://publicsuffix.org/list/effective_tld_names.dat
My initial thought was to regex match the file for suffix list but I don't know how to regex files in javascript. I'm kind of new to javascript.

Anyone having idea, how to achieve this. Thank You


Solution

  • First off, the list doesn't seem to be allowed to do cross-domain, so direct AJAX to the resource isn't possible.

    What you can do is have your server load it for you (PHP: file_get_contents, JS: http.get). Then, implement a parser. I'm not familiar with the format of the file, but you can read the file line by line, skip blank lines and lines with //. Then load them into an array (PHP: array_push, JS: Array.prototype.push), serialize (PHP: json_encode, JS: JSON.serialize) and ship as JSON to your app.