0

My JavaScript MQTT Client (Paho) is subscribed to a Topic on a MQTT Broker (Mosquitto). I use JSON.parse to convert the array to an object.

client.onMessageArrived = onMessageArrived;
var myObj = JSON.parse(message.payloadString);

The payload looks like this,


{"applicationID":"4","applicationName":"Sensor-Module-241","deviceName":"P-241","devEUI":"00face0173800241","rxInfo":[{"mac":"0004a30b0022813c","rssi":-22,"loRaSNR":10.2,"name":"PiXCL-G000","latitude":45.485524299999994,"longitude":-75.6969496,"altitude":54}],"txInfo":{"frequency":903000000,"dataRate":{"modulation":"LORA","bandwidth":500,"spreadFactor":8},"adr":true,"codeRate":"4/5"},"fCnt":10101,"fPort":99,"data":"AGhQAXMq+AJnAHgDAAEEAAEFAQEGAQEHAgEYCAIB0A==","object":{"digitalInput":{"3":1,"4":1},"digitalOutput":{"5":1,"6":1},"analogInput":{"7":2.8,"8":4.64},"temperatureSensor":{"2":12},"humiditySensor":{"0":40},"barometer":{"1":1100}}}

Using typeof I get,


 console.log('payLoadString: ', typeof 'payLoadString');//string
    console.log('message.payloadString: ', typeof message.payloadString);//string
    console.log('myObj: ', typeof myObj);//object
    console.log('myObj.applicationID: ', typeof myObj.applicationID);//string
    console.log('myObj.applicationName: ', typeof myObj.applicationName);//string
    console.log('myObj.deviceName: ', typeof myObj.deviceName);//string
    console.log('myObj.devEUI: ', typeof myObj.devEUI);//string
    console.log('myObj.rxInfo: ', typeof myObj.rxInfo);//object
    console.log('myObj.txInfo: ', typeof myObj.txInfo);//object
    console.log('myObj.fCnt: ', typeof myObj.fCnt);//number
    console.log('myObj.fPort: ', typeof myObj.fPort);//number
    console.log('myObj.data: ', typeof myObj.data);//string
    console.log('myObj.object: ', typeof myObj.object);//object

If I do the following,

  • my first console.log gives me an array???
  • I assign that array to a variable
  • my second console.log gives me an array???
  • my third console.log gives me an object???

I get stuff I do not understand,

console.log('Object.entries(myObj): ', Object.entries(myObj));
   var n = Object.entries(myObj);
   console.log('typeof: n: ', n);
   console.log('typeof: Object.entries(myObj): ', typeof Object.entries(myObj));

Object.entries(myObj):  
Array [ […], […], […], […], […], […], […], […], […], […] ]
utility.js:114:2
typeof: n:  
Array [ […], […], […], […], […], […], […], […], […], […] ]
utility.js:116:2
typeof: Object.entries(myObj):  object

I thought in the beginning that I had used JSON.parse to make the object, but now I don't know what it is. Then, I wanted to get the information (name:values) contained in rxInfo and also in the ??? that is named object (note: this entry follows the one with the name data) and I want to get the individual name:values from this as well, along with their name:values.

In summary, I want to get the name:value pairs from what I had thought was a json message from the mqtt broker, such that I can use the names in a column in a table and the corresponding values in the adjacent column of the table in a browser.

I keep trying but end up with various things that seem so weird. For instance, this Object.entries(myObj.rxInfo[0]) gives me

Array [ […], […], […], […], […], […], […] ]

and I can get the length, and use Object.entries(), Object.keys() and Object.values() on this, but when I try to access a specific entry using a string key, console.log(Object.entries(myObj.rxInfo[0].rssi)), I end up with

Array []

and when I do this,

console.log(Object.entries(myObj.rxInfo[0]).rssi);

I end up with

undefined

ARRGGGHHHH! I must admit I am not very knowledgeable about JavaScript, but had thought that this would not be very hard, and it's turning out to be quite confusing.

2
  • 2
    typeof <array> returns "object" because an array is just an object with a few special properties. Commented Apr 9, 2018 at 19:53
  • 2
    Object.entries returns an array, hence why you get an array.... typeof by the way doesn't give a "array" type string. Array's are just specialized objects hence why it gives "object" Commented Apr 9, 2018 at 19:53

1 Answer 1

2

Calling typeof on an array will return object which leads to confusion, in the example you provided rxInfo is an array. You can try myObj.rxInfo[0].rssi and you should see the value you expected.

You can check if the object is an actual array using isArray or testing the prototype Object.prototype.toString.call(data) == '[object Array]';

Sign up to request clarification or add additional context in comments.

1 Comment

That is the answer. As a beginner I find the concepts that I encountered here to be confusing. I suppose I was overthinking this and trying to make use of complex methods to solve the problem. In my non-understanding, as I was seeing something so close as to what I wanted I had thought the problem was far too complex for me. I know I should avoid saying thanks, but golly, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.