Please help with SyntaxError: JSON.parse in storage integration

Hello,

I’m trying to access the stored data from my GPS LW-001BG tracker Moko via storage integration.
SyntaxError error occurs: JSON.parse: unexpected non-whitespace character after JSON data at line 2 column 1 of the JSON data
In the V2 version, I was able to access the stored data via Swagger without any problems.
Please advise what to do with this error.

screen

Hi @Petr,

The main issue here is that the response type is not application/json but text/event-stream, which is a stream of JSON formatted messages (separated by empty lines). Here is an example response (quoted from the docs):

{"result":{"end_device_ids":{"device_id":"dev1","application_ids":{"application_id":"app1"},"dev_eui":"1111111111111111","dev_addr":"014457CB"},"received_at":"2020-08-24T10:08:44.868680817Z","uplink_message":{"session_key_id":"AXPoziFRvbcEguvZQoCCZw==","f_port":10,"f_cnt":43,"frm_payload":"AQoBCgEKAQo=","rx_metadata":[{"gateway_ids":{"gateway_id":"gtw1"},"time":"2020-08-24T10:08:43.385687165Z","timestamp":3313328983,"uplink_token":"ChIKEAoEZ3R3MRIIEREREREREREQ18b1qwwaDAiso476BRDl6YiuAiDYl7COt2A="}],"settings":{"data_rate":{"lora":{"bandwidth":125000,"spreading_factor":12}},"coding_rate":"4/5","frequency":"868100000","timestamp":3313328983,"time":"2020-08-24T10:08:43.385687165Z"},"received_at":"2020-08-24T10:08:44.634338856Z"}}}

{"result":{"end_device_ids":{"device_id":"dev1","application_ids":{"application_id":"app1"},"dev_eui":"1111111111111111","dev_addr":"014457CB"},"received_at":"2020-08-24T10:08:49.144907967Z","uplink_message":{"session_key_id":"AXPoziFRvbcEguvZQoCCZw==","f_port":10,"f_cnt":44,"frm_payload":"AQoBCgEKAQo=","rx_metadata":[{"gateway_ids":{"gateway_id":"gtw1"},"time":"2020-08-24T10:08:48.891099194Z","timestamp":3318834395,"uplink_token":"ChIKEAoEZ3R3MRIIEREREREREREQ28nFrgwaDAiwo476BRCoqe67AyD47sfPy2A="}],"settings":{"data_rate":{"lora":{"bandwidth":125000,"spreading_factor":12}},"coding_rate":"4/5","frequency":"868100000","timestamp":3318834395,"time":"2020-08-24T10:08:48.891099194Z"},"received_at":"2020-08-24T10:08:48.931407608Z"}}}

Attempting to parse this as a JSON object will raise the error message that you encounter.

Note that the default response message contains a lot of fields for the uplink message. You can limit the fields that are returned by specifying field mask paths in the request, e.g.:

curl -G "https://eu1.cloud.thethings.network/api/v3/as/applications/app1/packages/storage/uplink_message" \
    -H "Authorization: Bearer $API_KEY" \
    -H "Accept: text/event-stream" \
    -d "limit=10" \
    -d "field_mask=up.uplink_message.f_cnt,up.uplink_message.frm_payload,up.uplink_message.decoded_payload"

See also the documentation for more info on how to use the HTTP API to retrieve uplink messages persisted in the Storage Integration. Hope this helps!

@neoaggelos, to actually answer the question for @Petr:

Once you get the response, in Javascript you need:

response = '{"data": [' + response.replace(/\n\n/gm, ',').slice(0, -1) + ']}';

If you do Python for an application or service it is:

theJSON = "{\"data\": [" + r.text.replace("\n\n", ",")[:-1] + "]}";

The docs are good if you are really old (like me) & can see “The Matrix”. I get the whole Event Stream idea but as it does not have any CORS options in the browser it’s a b1tch to use and the gaps between results are catching people out with other code.

Thank you for your answers.
To be honest, I’m just an ordinary user who started researching LoRaWAN networks as a hobby. I could think of some uses for LoRaWAN devices that I could test in practice, and the TTS (TTN) network seems to me to be the best choice. Now I’m thinking of placing a gateway on the house.
Unfortunately, I don’t understand programming, so I don’t know what your advice means to me. Apparently there’s some wrong format.
Previously, I could easily view the history of data sent by my tracker on Swagger. It was useful for testing. Now it is not possible in the TTS network.
Would it be possible, please, to explain to me in a simple way how to get history back?
Otherwise, I successfully use the TagoIO application to display the location of the tracker on the map, but I would still like to see a functional storage integration.

Yes it is. But you have to set the limit to 1 as the issue is to do with the way the JSON is sent. If you set the sort to received data in reverse, you’ll get the latest one:

https://eu1.cloud.thethings.network/api/v3/as/applications/YOUR-APPLICATION-ID/devices/YOUR-DEVICE-ID/packages/storage/uplink_message?order=-received_at&limit=1

Not really, not without you learning a bit of code. If a supermodel can do it, or a US Senator who goes on to be the President can do it, I’m sure you can too.

On the plus side, I am working on such tools - I have already done it for Data Storage but it needs some tidy up and I’m waiting on an official answer to the short name of the new v3 stack - if anyone cares to prompt TTI on this, that would be super useful: How do we refer to the v3 Stack in third party offerings · Issue #4130 · TheThingsNetwork/lorawan-stack · GitHub

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