12

I am new to both Javascript and JSON worlds. I am wondering how I could convert an incoming Uint8Array data () to a JS object? Any help / pointers please. Here's what I have done as an experiment.

// arr is uint8Array incoming data
function myConvertFunc(arr) {
 let str = "";
 for (var i=0; i<arr.byteLength; i++) {
 str += String.fromCharCode(arr[i]);
 }
 // Say, 'str' at this step looks like below :
 /* {"type": "newEvent", "content": {"rec": [{"id1": "1", "event": "3A=","payload": "EZm9ydW0ub="}]}} */
 var serializedData = JSON.stringify(str);
 let message = JSON.parse(serializedData);
 switch (message.type) {
 case "newEvent":
 log("In newEvent");
 break;
 .
 .
 .
 default:
 log("undefined message type");
 }
}

Contrary to my understanding, the default case log : "undefined message type" is show in my logs. Could someone please help me figure out my mistake here?

asked May 24, 2013 at 0:01
4
  • 3
    What is a "Uint8Array"? Commented May 24, 2013 at 0:02
  • The Uint8Array type represents an array of 8-bit unsigned integers. Not sure if this is the answer you are looking for Commented May 24, 2013 at 0:04
  • @MattBall: It's a typed array. See developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/… Commented May 24, 2013 at 0:13
  • 1
    @Siddartha: Why do you need to put a JSON string into a typed array (or the other way round)? Commented May 24, 2013 at 0:14

1 Answer 1

5
var serializedData = JSON.stringify(str);
let message = JSON.parse(serializedData);

That means if there are no errors that str === serializedData (or at least two equal-looking objects).

Say, 'str' at this step looks like below:

{"type": "newEvent", "content": {"rec": [{"id1": "1", "event": "3A=","payload": "EZm9ydW0ub="}]}}

Now, if str is the JSON string then you just want

var message = JSON.parse(str);

Currently, you did JSON-encode and then -decode the JSON string, with the result that message was the string again and its type property was undefined.

answered May 24, 2013 at 0:19
Sign up to request clarification or add additional context in comments.

3 Comments

Well, this is what I have tried at the very beginning. When I JSON.parse(str), I get the error : "JSON.parse: unexpected non-whitespace character after JSON data". Hence unsure of this error, resorted to the code snippet that I first posted. But thanks though for clarifying.
Then probably your JSON string got amended with some invalid characters. Do a console.log(str) to make sure it is valid - not sure where you got your uint8array from and what else it might contain.
Thanks for identifying the issue. Indeed there was an extra character that was putting off JSON.parse(...). Now the issue is fixed. Appreciate your time.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.