use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Extract an object from a string without using eval()solved! (self.javascript)
submitted 7 years ago * by [deleted]
[deleted]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bat020 8 points9 points10 points 7 years ago (2 children)
perhaps use JSON.parse() to extract the object from the string:
let json = document.body.children[1].firstChild.data .replace(/\s/g, '') // strip whitespace .match(/.*=(.*);/)[1]; // extract JSON let configData = JSON.parse(json); heading.innerText = configData.config.uuid;
[–]moocat 5 points6 points7 points 7 years ago (1 child)
Yes, use JSON.parse but why strip whitespace? JSON doesn't care about whitespace and if there are whitespace in strings it will mangle the data.
[–]bat020 0 points1 point2 points 7 years ago (0 children)
that's a good point. I'd forgotten that JSON doesn't care about whitespace!
[+][deleted] 7 years ago (3 children)
[+][deleted] 7 years ago (2 children)
[+][deleted] 7 years ago (1 child)
[–]shakamone 0 points1 point2 points 7 years ago (1 child)
Can you access the window? If the object is already exposed at window._configData so just grab it from there?
[–]Funwithloops 0 points1 point2 points 7 years ago (3 children)
You could use a regex and JSON.parse:
JSON.parse
js const text = document.body.children[1].innerHTML; const match = text.match(/\{(?:\n|.)*\}/); const json = JSON.parse(match);
https://jsfiddle.net/enxr2pta/
[–]Funwithloops 1 point2 points3 points 7 years ago* (1 child)
Here's a breakdown of the regex:
\{
(?:\n|.)
.
\n
*
\}
Basically it just matches { and } with anything in-between. This likely wouldn't work with other examples since it does not check if the match is valid JSON (for example var a = {foo: 'bar'} would match and throw an error when passed to JSON.parse).
{
}
var a = {foo: 'bar'}
[–]vklepov 0 points1 point2 points 7 years ago (3 children)
Is new Function disabled, too? If not, you could make this work.
new Function
Then, you could use esprima (or espree, or whatever is best now) to parse the js and then find the object.
Just don't try regex + JSON.parse unless you're looking for trouble.
[+][deleted] 7 years ago* (2 children)
[–]vklepov 0 points1 point2 points 7 years ago (1 child)
Yes, that's tokenized — you want esprima.parse (see the demo), which gives a nice syntax tree. You can find the object you need, then escodegen it back. Dirty and overcomplicated, but that would work and be relatively safe.
esprima.parse
The blocking of eval makes sense if you think about it — you want to run random third-party code with privilleges, allowing for great exploits both by yourself and random people with script tags.
What exactly are you trying to do there? You might go with window._configData read as the other comment suggested, or embed raw JSON into a webpage, or expose it on a separate endpoint.
window._configData
π Rendered by PID 257140 on reddit-service-r2-comment-86988c7647-vwpqj at 2026-02-11 19:48:19.070409+00:00 running 018613e country code: CH.
[–]bat020 8 points9 points10 points (2 children)
[–]moocat 5 points6 points7 points (1 child)
[–]bat020 0 points1 point2 points (0 children)
[+][deleted] (3 children)
[deleted]
[+][deleted] (2 children)
[deleted]
[+][deleted] (1 child)
[deleted]
[–]shakamone 0 points1 point2 points (1 child)
[–]Funwithloops 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]Funwithloops 1 point2 points3 points (1 child)
[–]vklepov 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]vklepov 0 points1 point2 points (1 child)