# Website returns "var temp = "24";" How to parse such a string into json

**URL:** <https://discourse.nodered.org/t/website-returns-var-temp-24-how-to-parse-such-a-string-into-json/13142>\
**Category:** General\
**Created:** [11 July 2019 01:46 UTC](https://discourse.nodered.org/t/website-returns-var-temp-24-how-to-parse-such-a-string-into-json/13142 "2019-07-11T01:46:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kentsor](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kentsor](https://discourse.nodered.org/u/kentsor)\
**Post date:** [11 July 2019 01:46 UTC](https://discourse.nodered.org/t/website-returns-var-temp-24-how-to-parse-such-a-string-into-json/13142/1 "2019-07-11T01:46:00Z")

</div>

I'm trying to pull data out of the official Canadian weather service website. There is a script used on the site that returns the values used by the rest of the scripts to format the page as a javascript variable declaration.

var obTemperature = "24";  
var obIconCode = "29";  
var obCondition = "";  
var obWindDir = "SE";  
var obWindSpeed = "10";  
var obWindGust = "";  
var obWindChill = "";  
var obHumidex = "28";

What is the best way to turn this into a json object I can more easily use in node red ? I was thinking it could be sent into a function but don't know how to turn such text into javascript that is executed ?

I'm aware of openweathermap, wunderground etc. but I'd like to use this.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 July 2019 02:54 UTC](https://discourse.nodered.org/t/website-returns-var-temp-24-how-to-parse-such-a-string-into-json/13142/2 "2019-07-11T02:54:07Z")

</div>

Can you post the debug output so we can see the actual output.

---

<div class="post-metadata">

**Author:** ![tilleul](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/tilleul/32/78_2.png) [@tilleul](https://discourse.nodered.org/u/tilleul)\
**Post date:** [11 July 2019 06:01 UTC](https://discourse.nodered.org/t/website-returns-var-temp-24-how-to-parse-such-a-string-into-json/13142/3 "2019-07-11T06:01:07Z")

</div>

I would use a function node (maybe jsonata would work too -- it's rather powerful -- but I don't know much about jsonata).

If you've received the following declarations as a string, I would do a series of `replace()` on the string. You can use regex which is of great help.

- replace all the `var` with `"` (quote)

```auto
data = data.replace(/var/g,'"');

```

- replace all the `;` characters with `,` (comma)

```auto
data = data.replace(/;/g, ",");

```

- replace all the `=` (with leading space) characters with `":` (a quote AND a colon)

```auto
data = data.replace(/ =/g, '":');

```

- now we must remove all the quotes surrounding numbers (but not the ones surrounding strings)

```auto
data = data.replace(/"(\d+)"/g, "$1");

```

or maybe, if you have floating numbers you can use this one instead:

```auto
data = data.replace(/"([\d.]+)"/g, "$1");

```

We now have something like this

```auto
 "obTemperature": 24,
 "obIconCode": 29,
 "obCondition": "" ,
 "obWindDir": "SE",
 "obWindSpeed": 10,
 "obWindGust": "" ,
 "obWindChill": "" ,
 "obHumidex": 28,

```

- remove that last comma with

```auto
data = data.substr(0, data.lastIndexOf(","))

```

- add a leading `{` and trailing `}`

```auto
data = "{" + data + "}";

```

- and now convert to json

```auto
let json = JSON.parse(data);

```

I think I got it all 😉

---

<div class="post-metadata">

**Author:** ![kentsor](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@kentsor](https://discourse.nodered.org/u/kentsor)\
**Post date:** [11 July 2019 16:02 UTC](https://discourse.nodered.org/t/website-returns-var-temp-24-how-to-parse-such-a-string-into-json/13142/4 "2019-07-11T16:02:35Z")

</div>

Thanks for the reply, the last line was really the conceptual stumbling block for me, the grep expressions doesn't seem to work quite right, but that's something I can work on. Thanks again!
