0

In my node app i am using mysql. I retrieve data from mysql and converting each row to json(working fine).But my requirement i have to print the field with value otherwise don't.

Consider: My sql result..

level0:"xxxxxx"  level1:"fffffff" level3:"btggtgt" level:"" 

Now the json i am getting is:

[
{    
level0="xxxxxxx",
level1="fffffff",
level3="btggtgt".
level4=""
}
]

But i need the output as:

[
    {
    level0="xxxxxxx",
    level1="fffffff",
    level3="btggtgt"    
    }
    ]

i have to omit level4 because its value is ""(empty).

My code is:

connection.query("select * from levels where level0 = '" + level0 + "' and level1 = '"+level1+"' and level2 = ''", function(err, row1,          fields)    {

    res.writeHead(200, { 'Content-Type': 'application/json'});
    res.end(JSON.stringify(row1,null,"\n"));

    }); 


var query5result;
    querylevel5(function(result5)
    {
for (var level in result5)
{
console.log("ssssssss="+util.inspect(result5[level]))
  if (result5[level] == null || result5[level] == '')
  {
      delete result5[level];
  }
}
query5result=result5;
17
  • store the mysql result in an array then filter it and do as you wish Commented Nov 29, 2013 at 5:32
  • in your table, is level "blank" or is it "NULL" Commented Nov 29, 2013 at 5:33
  • Thanks @Rafee i think mysql result itself an array..How can i filter and apply json?? Commented Nov 29, 2013 at 5:35
  • can any level be black randomly? Commented Nov 29, 2013 at 5:35
  • yes...... om_deshpande ... Commented Nov 29, 2013 at 5:36

1 Answer 1

0

Here is a way to loop and delete this empty level

connection.query("select * from levels where level0 = '" + level0 + "' and level1 =    '"+level1+"' and level2 = ''", function(err, row1,          fields)    {
for (var level in row1)
{
  if (row1[level] == null || row1[level] == "")
  {
      delete row1[level];
  }
}
)};
//...then go on:
res.end(JSON.stringify(row1,null,"\n"));
Sign up to request clarification or add additional context in comments.

1 Comment

you already had queryed the rows in the first line or not? so look in my edit

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.