-1

Can anyone help me in this.

[
 "{proId: 845,particularProduct: 1153,parQuantity: 2,rPrice: 899,numItems: 100}",
 "{proId: 848,particularProduct: 1157,parQuantity: 2,rPrice: 1009,numItems: 3}"
]

How Can I get the value of proId, particularProduct, etc

Dilip
  • 3
  • 2
  • 4
    Fix whatever's serving you that essentially broken input so that it gives you proper JSON instead, and then you can JSON.parse it – CertainPerformance Sep 21 '19 at 11:31
  • Possible duplicate of [How can I pretty-print JSON using JavaScript?](https://stackoverflow.com/questions/4810841/how-can-i-pretty-print-json-using-javascript) – Siddharth Das Sep 21 '19 at 11:39

2 Answers2

2

Loop through the array and use eval() method to make elements as object:

var data = [
 "{proId: 845,particularProduct: 1153,parQuantity: 2,rPrice: 899,numItems: 100}",
 "{proId: 848,particularProduct: 1157,parQuantity: 2,rPrice: 1009,numItems: 3}"
]

data.forEach(function (item) {
   var parsedData = eval('(' + item + ')');
   console.log(parsedData.proId)
})

Output:

845
848
Bilal Siddiqui
  • 3,579
  • 1
  • 13
  • 20
0

The JSON is correct, in a way: it is an array with two strings in it. These two strings have a certain resemblance with JSON again, but are not valid JSON. However, the whole thing can be parsed with

arr.forEach((el,i)=>arr[i]=eval('('+el+')'))
console.log(arr[0].proId,arr[0].particularProduct);
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43