I'm making a web scraper, most of the data on the web page is in JavaScript object literal form, e.g.:
// Silly example
var user = {
name: 'John',
surname: 'Doe',
age: 21,
family: [
{
name: 'Jane',
surname: 'Doe',
age: 37
},
// ...
]
};
So when I search for the contents in my JavaScript app the Object above would be:
"{name: 'John', surname: 'Doe', age: 21, family: [{name: 'Jane', surname: 'Doe', age: 37}]}"
Is it possible to parse those to regular JavaScript Objects without using 'eval' or making my own parser? I saw other similar questions about this but the answers are not applicable: they all suggest JSON.parse()
(not applicable) and eval
(I can't use it for security reasons). In this question, for example, all the answers suggest eval
or new Function()
which are basically the same thing.
If there are no other ways would it be a viable option to convert the literal to proper JSON and then parse it to JavaScript object?
This is what I tried right now, it worked on a simple object but I'm not sure it will work everywhere:
const literal = script.innerText.slice(script.innerText.indexOf('{'), script.innerText.lastIndexOf('}') + 1);
const json = literal.replace(/.*:.*(\".*\"|\'.*\'|\[.*\]|\{.*\}|true|false|[0-9]+).*,/g, (prev) => {
let parts = prev.split(':');
let key = '"' + parts.shift().trim() + '"';
let value = parts.join(':').replace(/'.*'/, (a) => {
return '"' + a.slice(1, a.length - 1) + '"';
}).trim();
return key + ':' + value;
});
const obj = JSON.parse(json);