# Unexpected token when trying to convert to JSON

**URL:** <https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090>\
**Category:** General\
**Created:** [13 June 2021 04:34 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090 "2021-06-13T04:34:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheOzarkWizard](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@TheOzarkWizard](https://discourse.nodered.org/u/TheOzarkWizard)\
**Post date:** [13 June 2021 04:34 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090/1 "2021-06-13T04:34:17Z")

</div>

Hello, I am switching to Adafruit's python library because pimoroni did not include a sea level/altitude parameter. For some reason, I cannot convert the output to a JSON object. What am I missing?

![Capture](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/e/6e0e828c22fc7c2d72943ca940ce3c100434d582.png)

 ![Capture2](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/9/e9acbd5519962e90415b9bc06ab4fc3c336a777c.png)

```auto
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_bme680

# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
bme680 = adafruit_bme680.Adafruit_BME680_I2C(i2c, debug=False)

# change this to match the location's pressure (hPa) at sea level
bme680.sea_level_pressure = 1013.25

# You will usually have to add an offset to account for the temperature of
# the sensor. This is usually around 5 degrees but varies by use. Use a
# separate temperature sensor to calibrate this one.
temperature_offset = -5

while True:
    print("\nTemperature: %0.1f C" % (bme680.temperature + temperature_offset))
    print("Gas: %d ohm" % bme680.gas)
    print("Humidity: %0.1f %%" % bme680.relative_humidity)
    print("Pressure: %0.3f hPa" % bme680.pressure)
    print("Altitude = %0.2f meters" % bme680.altitude)

    time.sleep(1)
```

---

<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:** [13 June 2021 07:20 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090/2 "2021-06-13T07:20:54Z")

</div>

The output you are generating is a string, not json.

Could you try something like:

```auto
import json

...

print(json.dumps({
"Temperature": %0.1f C" % (bme680.temperature + temperature_offset),
"Gas: %d ohm" % bme680.gas,
"Humidity: %0.1f %%" % bme680.relative_humidity,
"Pressure: %0.3f hPa" % bme680.pressure,
"Altitude: %0.2f meters" % bme680.altitude
}))

```

---

<div class="post-metadata">

**Author:** ![TheOzarkWizard](https://avatars.discourse-cdn.com/v4/letter/t/4da419/32.png) [@TheOzarkWizard](https://discourse.nodered.org/u/TheOzarkWizard)\
**Post date:** [13 June 2021 07:40 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090/3 "2021-06-13T07:40:58Z")

</div>

returns syntax error:  
 ![syntax](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/c/fcfc40c5fda3b29ec45e852d0a44db2b3047dd72.png)

---

<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:** [13 June 2021 07:55 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090/4 "2021-06-13T07:55:27Z")

</div>

I forgot some quotes and it probably needs to all be on the same line, not sure how python interpretes it.

```auto
print(json.dumps({"Temperature": "%0.1f C" % (bme680.temperature + temperature_offset),"Gas": "%d ohm" % bme680.gas,"Humidity": "%0.1f %%" % bme680.relative_humidity,"Pressure": "%0.3f hPa" % bme680.pressure,"Altitude": "%0.2f meters" % bme680.altitude}))

```

Also not sure how the printf formatting is being interpreted inside json.dump, probably need to set variables first.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [13 June 2021 09:33 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090/5 "2021-06-13T09:33:08Z")

</div>

Here is a link to an explanation of using Json in python that might help you  
[https://www.programiz.com/python-programming/json](https://www.programiz.com/python-programming/json)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [12 August 2021 09:33 UTC](https://discourse.nodered.org/t/unexpected-token-when-trying-to-convert-to-json/47090/6 "2021-08-12T09:33:55Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
