0

this my code

app.get('/status',function ( req,res) {
        var data = {
            "error": 1,
            'data status': ""
        };
        connection.query("SELECT * from status", function (err, rows, fields) {
            if (rows.length != 0) {
            data = rows;
                console.log(data);
                res.json(data);
             } else {
                data["Data status"] = '0';
                res.json(data);
            }
        })
});

I had a problem I try to change the json object

[{"id_status":5,"pin_arduino":1,"status":0}] 

I will eliminate be

{"id_status":5,"pin_arduino":1,"status":0}

6
  • Does JSON.parse not work? Commented Mar 6, 2017 at 9:35
  • Can you elaborate more what is that you want to acheive ? Commented Mar 6, 2017 at 9:43
  • yes i'am try JSON.parse, JSON.stringify not work @evolutionxbox Commented Mar 6, 2017 at 9:44
  • Why did you use stringify? Please read developer.mozilla.org/docs/Web/JavaScript/Reference/… Commented Mar 6, 2017 at 9:45
  • I tried to remove the existing array format in json @damitj07 Commented Mar 6, 2017 at 9:46

1 Answer 1

1

It's not clear what you're asking for.

If you get this:

[{"id_status":5,"pin_arduino":1,"status":0}]

and you want this:

{"id_status":5,"pin_arduino":1,"status":0}

then instead of:

res.json(data);

use:

res.json(data[0]);

If you get this:

{"id_status":5,"pin_arduino":1,"status":0}

and you want this:

[{"id_status":5,"pin_arduino":1,"status":0}]

then instead of:

res.json(data);

use:

res.json([data]);

Also, looking at your examples, this:

data["Data status"] = '0';

should probably be:

data.status = 0;

or:

data[0].status = 0;

depending on how your data really looks like, which is not entirely clear from your question.

Note that you're not doing anything with JSON here. You just handle normal JS objects which get serialized to JSON automatically by Express when you use res.json() so there's no need to use JSON.parse() or JSON.stringify() at all, at least not in the code that you included in your question.

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

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.