# Cheerio - Capturing value from a JS variable?

**URL:** <https://community.postman.com/t/cheerio-capturing-value-from-a-js-variable/5536>\
**Category:** Help Hub\
**Tags:** cheerio\
**Created:** [May 22, 2019, 8:00am UTC](https://community.postman.com/t/cheerio-capturing-value-from-a-js-variable/5536 "2019-05-22T08:00:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![postman-paul540](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/postman-paul540/32/149_2.png) [@postman-paul540](https://community.postman.com/u/postman-paul540)\
**Post date:** [May 22, 2019, 8:00am UTC](https://community.postman.com/t/cheerio-capturing-value-from-a-js-variable/5536/1 "2019-05-22T08:00:21Z")

</div>

Hey, I’ve started using Cheerio for the first time and I’ve tried Googling around but can’t seem to find an answer to this, or if it’s even possible.

I want to grab the “int\_id” value that’s inside the variable “mt1\_instance”.  
How can I do this? 😄  
I tried playing about using Cheerio, but I just keep crashing Postman so it’s really hard to trial and error this sort of thing.

![image](https://us1.discourse-cdn.com/getpostman/original/3X/c/4/c45ebc9cd430803333e40c405540abe4780d64f6.png)

---

<div class="post-metadata">

**Author:** ![VincentD123](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/vincentd123/32/1845_2.png) [@VincentD123](https://community.postman.com/u/VincentD123)\
**Post date:** [May 22, 2019, 10:00am UTC](https://community.postman.com/t/cheerio-capturing-value-from-a-js-variable/5536/2 "2019-05-22T10:00:17Z")

</div>

HI @postman-paul540

Nothing like a challenge… how’s this for you?

**Using**

```plaintext
const htmlResp = 
`<div id="app_body">
	<div id="app_content">
		<script language="javascript">
			// we need to providean access to the layout mode to the ...
			var bol_pos_mode = false;
			
			var mt1_instance = {
				int_id:1111911,
				str_hash:"gobbledigook",
				int_hub:1 };
				
			// preload functionality
			Ext.onReady(function() {
						});
			
			var DR_ALT_BANK_REFUNDS = 1;
		</script>
	</div>
</div>`; 

```

**code**

```javascript
const htmlAsJson = xml2Json(htmlResp); console.log(htmlAsJson);
const scriptStr = htmlAsJson.div.div.script._; console.log(scriptStr);
// you could use a bit more short circuiting here if you were in any doubt 
// whether the expected elements are likely to be there or not

const matches = scriptStr.match(/var mt1_instance = (\{[\s\S]*?\});/); console.log(matches);
// regex looking for the lines that define mt1_instance 
// with capturing group for the {}
// if a match was found, it is in matches[0], 
// and the capturing group in matches[1]

try { // json parse the variable string
	var mt1_instance = matches && matches[0] && matches[1] 
		&& JSON.parse(matches[1]);
}catch(e){ console.log(e); /* ignore paring errors*/ }

if(!mt1_instance){
	console.log("could not parse, will try eval instead");
	matches && matches[0] && eval(matches[0]);
}

mt1_instance && console.log("mt1_instance.int_id: " + mt1_instance.int_id);

```

produces output

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/f/c/fc98eb3f1fcaf0fed29186bc5e5d23b0fb8eb4ba.png)

I would have preferred not to use the `eval()` but I used it because the variable does not parse as JSON.

It doesn’t use Cheerio.js and there will be other ways to manipulate the html, but I like keeping it in javascript as much as possible, hence I used `xml2Json()`.

Hope that gives you some ideas to try out @postman-paul540 🤠

See [short circuiting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators#Short-circuit_evaluation) for more info.
