0

i want to parse the following kind of data in the node js in order to perform operations on them. the data :-

{"orderSummary":[ {
      "name":"Wine",
      "ProductPrice":"500",
      "ProductQuantity":"2",
      "ProductCost":"1000",
      "SellerId":"2"

   },{
      "name":"Whisky",
      "ProductPrice":"500",
      "ProductQuantity":"2",
      "ProductCost":"1000",
      "SellerId":"2"

   }]
}

here is my code snippet which iam using to parse the data :-

router.post('/createorder',function(req,res){
    console.log(req.body);
     var data=req.body;
    var obj=JSON.parse(data);
    res.send(obj);
});

can anybody tell me what i can do besides looping (as i will implement it ) in order to parse the data and perform operations on it ?

4
  • i have gone through this but still i'm unable to find exact answer as my data is coming from an app as key value par showing in the input data Commented Nov 10, 2017 at 19:49
  • @atulkumar Please, be more clear about what are you trying to accomplish. Commented Nov 10, 2017 at 19:50
  • It depends on what parsing an operations you want to do... Be concrete. Commented Nov 10, 2017 at 19:52
  • i want to access the elements and store it in the database Commented Nov 10, 2017 at 19:59

1 Answer 1

0
{"orderSummary":[ {
  "name":"Wine",
  "ProductPrice":"500",
  "ProductQuantity":"2",
  "ProductCost":"1000",
  "SellerId":"2"

},{
  "name":"Whisky",
  "ProductPrice":"500",
  "ProductQuantity":"2",
  "ProductCost":"1000",
  "SellerId":"2"

}]
}

Assuming this json data is in body so just store in var var data=req.body.orderSummary now iterate through it. Thats the best way as you have json data.So you need to implement it.

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

6 Comments

i have tried that and iam getting the following error " SyntaxError: Unexpected token # in JSON at position 0"
var data=req.body.orderSummary ; for (var i=0;i<data.length;i++){ console.log(data[i]); }; // this code is working try it
It works for you?
Please upvote my answer.
already did that
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.