1

I'm attempting to convert an array to JSON to be sent to a client. Here is what the data looks like in console:

[ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],                                                                                        
LU: [ true,                                                                                                                                                            
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
false,                                                                                                                                                               
false,                                                                                                                                                               
false ],                                                                                                                                                             
SE: [ false, false, false ] ] 

However when I run this (res being an express.js socket):

console.log(st.bot.serverStatus);
res.send(JSON.stringify(st.bot.serverStatus));

I get the output in console like expected, but I get [] from the web browser. What am I doing wrong?

PS: I am unable to change the format of the elements, they are generated by this method:

        if(st.bot.serverStatus[tmp.country] !== undefined) {
            st.bot.serverStatus[tmp.country][st.bot.serverStatus[tmp.country].length] = alive;
        } else {
            st.bot.serverStatus[tmp.country] = [ alive ];
        }
3
  • Can you add the output you get from the server? Commented Apr 15, 2015 at 0:49
  • 1
    How are you initializing st.bot.serverStatus ? Commented Apr 15, 2015 at 0:58
  • st.bot.serverStatus = []; Commented Apr 15, 2015 at 2:41

2 Answers 2

1

It's not valid syntax at SE: and LE: since it is an array and not an object. Change the outermost [] to {} or change : to ,

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

Comments

0

Use either:

console.log(JSON.stringify({ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ],                                                                                        
LU: [ true,                                                                                                                                                            
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
false,                                                                                                                                                               
false,                                                                                                                                                               
false ],                                                                                                                                                             
SE: [ false, false, false ] } ));

Or:

console.log(JSON.stringify([{ NL: [ true, true, true, true, true, true, true, true, true, true, true, true ]},                                                                                        
{LU: [ true,                                                                                                                                                            
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
true,                                                                                                                                                                
false,                                                                                                                                                               
false,                                                                                                                                                               
false ]},                                                                                                                                                             
{SE: [ false, false, false ] }] ));

Your JSON format is incorrect.

Use something like

jsonformatter.curiousconcept.com

or

jsoneditoronline.org

To validate your JSONs

Comments

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.