sorting a JSON in Node.js

I have been looking for a couple of days for the answer for this and nothing seems to work. Im kinda new to node.js and I'm working on only the server side to answer POST from the client side. This is kinda like an assignment that i have to do. What i need to do is when the client script, that i did not write, makes a POST request at location '/sort' with parameter theArray, i need to sort the array removing all non-string values and return the resulting value as JSON. The client script will send theArray parameter in a stringified JSON Array. So something like this

{"theArray":"[[],\"d\",\"B\",{},\"b\",12,\"A\",\"c\"]"}.

I have tried this code here:

case '/sort':
        if (req.method == 'POST') {
            res.writeHead(200,{
                'Content-Type': 'application/json'
            });
            var fullArr = "";
                req.on('data', function(chunk) {
                    fullArr += chunk;
                    });
                req.on('end', function() {
                            var jPar = JSON.parse(fullArr);
                            var arr = "";
                            var par = jPar.theArray;
                            arr += par;

                                function censor(key, value) {
                                    if (typeof value == "string") {
                                            return value;
                                        } 
                                        return undefined;
                                        }
                        var jsonString = JSON.stringify(par, censor);
                        console.log(jsonString);
                });         
                    res.end();


        };

break;

but it returns this with an error:

undefined:1
%2C%22A%22C%22
^
SyntaxError: Unexpected token h
    at Object.parse (native) ...

Can someone please help me and if you can show me some code to help. Thanks

you need to decode POST data according to transport encoding (I believe it's application/x-www-form-urlencoded in your case)

var json = require('querystring').parse(fullArr).formParamName;

replace formParamName with actual name used in your POST form. Here is querystring module documentation

function parseBody (req, callback) {
  var buffer = '';
  req.on('data', function(chunk) {
    buffer += chunk;
  });
  req.on('end', function() {
    try {
      callback(null, JSON.parse(buffer));
    } catch(e) {
      callback(e);
    }
  });
}

function stringFilter(element) {
  return typeof element === 'string';
}

function requestHandler (req, res) {
  parseBody(req, function(err, body) {
    if(err) {
      res.writeHead(400, {
        'Content-Type': 'application/json'
      });
      return res.end(err.toString());
    }

    res.writeHead(200,{
      'Content-Type': 'application/json'
    });

    var result = body.theArray.filter(stringFilter).sort();

    res.end(JSON.stringify(result));
  });
}

require('http').createServer(requestHandler).listen(3000);

I have left out the url handling, this is just for parsing the body, filtering out non-strings and I also added the sort().

You can test this with (if you name the file app.js):

node app.js

curl -H "Content-Type: application/json" -d '{"theArray": [[], "a", {}, "c", "B"]}' localhost:3000

Edit: This requires the input (request body) be valid JSON, though. If it looks like the data in the question then you would have to write a parser for that format. To me, it looks like somebody had an array such that

var arr = [[],"d","B",{},"b",12,"A","c"];

And then did

JSON.stringify({theArray: JSON.stringify(arr)});