# Noob JSON issues

**URL:** <https://discourse.nodered.org/t/noob-json-issues/39771>\
**Category:** General\
**Created:** [26 January 2021 11:32 UTC](https://discourse.nodered.org/t/noob-json-issues/39771 "2021-01-26T11:32:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorenjz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lorenjz/32/35952_2.png) [@lorenjz](https://discourse.nodered.org/u/lorenjz)\
**Post date:** [26 January 2021 11:32 UTC](https://discourse.nodered.org/t/noob-json-issues/39771/1 "2021-01-26T11:32:04Z")

</div>

The following JSON is passed to Node-RED from a traccar server:

`{"event":{"id":683,"attributes":{},"deviceId":1,"type":"deviceUnknown","serverTime":"2021-01-26T03:31:58.515+0000","positionId":0,"geofenceId":0,"maintenanceId":0},"device":{"id":1,"attributes":{},"groupId":0,"name":"Lorens Phone","uniqueId":"XXXXXX","status":"unknown","lastUpdate":"2021-01-26T03:21:58.505+0000","positionId":91959,"geofenceIds":[1,2],"phone":"","model":"","contact":"","category":null,"disabled":false},"users":[{"id":2,"attributes":{"speedUnit":"mph","distanceUnit":"mi","timezone":"America/Chicago","notificationTokens":"tokenstuffhere"},"name":"Loren","login":"","email":"myemail","phone":"","readonly":false,"administrator":false,"map":"custom","latitude":36.339871,"longitude":-86.557218,"zoom":18,"twelveHourFormat":false,"coordinateFormat":"","disabled":false,"expirationTime":null,"deviceLimit":-1,"userLimit":0,"deviceReadonly":false,"token":null,"limitCommands":false,"poiLayer":"","password":null}]}`

I would like to send the information via telegram to my phone for debugging. I'm getting an error:

> TypeError: Cannot read property 'type' of undefined

Here is the function that is causing the error:

```auto
var incomming = msg.payload;

if(incomming.event["type"] && incomming["geofence"]){
    if(incomming.event.type === "geofenceEnter" || "geofenceExit"){
        var name = incomming.device.name
        var fence = incomming.geofence.name
        if(incomming.event.type === "geofenceEnter"){
            var direction = "Entered"
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: name + " " + direction + " " + fence
            }
        }
        else if(incomming.event.type === "geofenceExit"){
            var direction = "exited"
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: name + " " + direction + " " + fence    
            }
        }
        else if(incomming.event.type === "deviceOnline"){
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: name + " " + incomming.event.type
            }
        }else{
            msg.payload ={
                chatId:'XXXXXX',
                type: 'message',
                content: incomming
            }
        }
        
    }
    else{
        msg.payload ={
            chatId:'XXXXXX',
            type: 'message',
            content: incomming
        }
    }
    
    
}
else{
msg.payload ={
    chatId:'XXXXXX',
    type: 'message',
    content: incomming
    }
}
return msg;

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [26 January 2021 12:01 UTC](https://discourse.nodered.org/t/noob-json-issues/39771/2 "2021-01-26T12:01:26Z")

</div>

> [@lorenjz](#):
>
> Here is the function that is causing the error:

Put a debug node BEFORE this function, is there a `msg.payload.event.type` in the debug message?

* * *

This will not work (` || "geofenceExit"` will **always** evaluate to `true`)...

> [@lorenjz](#):
>
> `if(incomming.event.type === "geofenceEnter" || "geofenceExit")`

do this instead...  
`if(incomming.event.type === "geofenceEnter" || incomming.event.type === "geofenceExit")`

* * *

try this...

```auto
var incomming = msg.payload;

//check parts are present in payload - warn if not.
// NOTE: you might want to add or remove checks as suited by your application
// I would probably check that `incomming` is an object and that `incomming.event` is an object
if(!incomming || !incomming.event || !incomming.geofence) {
    node.warn(["invalid payload", incomming]);
    return;
}

//default message to send
msg.payload = {
            chatId:'XXXXXX',
            type: 'message',
            content: JSON.stringify(incomming)
        }
        
if(incomming.event.type && incomming.geofence){
    if(incomming.event.type === "geofenceEnter" || incomming.event.type === "geofenceExit"){
        let name = incomming.device.name
        let fence = incomming.geofence.name
        if (incomming.event.type === "geofenceEnter") {
            msg.payload.content = name + " Entered " + fence //update content
        } else if (incomming.event.type === "geofenceExit"){
            msg.payload.content = name + " exited " + fence //update content
        } else if (incomming.event.type === "deviceOnline"){
            msg.payload.content = name + " " + incomming.event.type
        } else {
            msg.payload.content = name + " " + JSON.stringify(incomming) //update content
        }
    } 
} 

return msg;

```

---

<div class="post-metadata">

**Author:** ![lorenjz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lorenjz/32/35952_2.png) [@lorenjz](https://discourse.nodered.org/u/lorenjz)\
**Post date:** [26 January 2021 13:10 UTC](https://discourse.nodered.org/t/noob-json-issues/39771/3 "2021-01-26T13:10:06Z")

</div>

> [@Steve-Mcl](#):
>
> ```auto
> if(incomming.event.type && incomming.geofence){
> if(incomming.event.type === "geofenceEnter" || incomming.event.type === "geofenceExit"){
> let name = incomming.device.name
> let fence = incomming.geofence.name
> if (incomming.event.type === "geofenceEnter") {
> msg.payload.content = name + " Entered " + fence //update content
> } else if (incomming.event.type === "geofenceExit"){
> msg.payload.content = name + " exited " + fence //update content
> } else if (incomming.event.type === "deviceOnline"){
> msg.payload.content = name + " " + incomming.event.type
> } else {
> msg.payload.content = name + " " + JSON.stringify(incomming) //update content
> }
> } 
> } 
> 
> return msg;
> 
> ```

@Steve-Mcl thanks so much. This looks really good. I've implemented it and see how today goes and let you know what I find.

---

<div class="post-metadata">

**Author:** ![lorenjz](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/lorenjz/32/35952_2.png) [@lorenjz](https://discourse.nodered.org/u/lorenjz)\
**Post date:** [29 January 2021 19:34 UTC](https://discourse.nodered.org/t/noob-json-issues/39771/4 "2021-01-29T19:34:58Z")

</div>

This is working really well thanks for your help!

---

<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 February 2021 19:35 UTC](https://discourse.nodered.org/t/noob-json-issues/39771/5 "2021-02-12T19:35:50Z")

</div>

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