-4

Based on the JSON data format below, how can we parse it where in I only wanted to get certain key for example I just wanted to get name and priority

[  
   "{'id': 12, 'category_name': 'BIR', 'priority': 1, 'category': 12, 'name': 'BIR FORMS'}",
   "{'id': 14, 'category_name': 'Contribution', 'priority': 0, 'category': 13, 'name': 'Pag-Ibig'}",
   "{'id': 13, 'category_name': 'Contribution', 'priority': 0, 'category': 13, 'name': 'SSS'}"
]
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Jhon Caylog
  • 483
  • 8
  • 24

3 Answers3

1

If you have no other quotes inside your data, you could replace single quotes ' with double quotes " to get a JSON compliant string.

Then parse the string, get the wanted properties and map new objects.

var strings = [  
        "{'id': 12, 'category_name': 'BIR', 'priority': 1, 'category': 12, 'name': 'BIR FORMS'}",
        "{'id': 14, 'category_name': 'Contribution', 'priority': 0, 'category': 13, 'name': 'Pag-Ibig'}",
        "{'id': 13, 'category_name': 'Contribution', 'priority': 0, 'category': 13, 'name': 'SSS'}"
    ],
    data = strings.map(s => JSON.parse(s.replace(/'/g, '"'))),
    selected = data.map(({ name, priority }) => ({ name, priority }));

console.log(selected);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

Let's assume that you have valid json data. You can do the following example:

const json = `[
   {"priority": 1, "name": "BIR FORMS"},
   {"priority": 0, "name": "Pag-Ibig"},
   {"priority": 0, "name": "SSS"}
]`;
const parsedJson = JSON.parse(json);

console.log(`${parsedJson[0].name}, ${parsedJson[0].priority}`);
FloWy
  • 934
  • 6
  • 14
-2

try to map the data as bellow,

 let data=[
"{'id': 12, 'category_name': 'BIR', 'priority': 1, 'category': 12, 'name': 
'BIR FORMS'}", "{'id': 14, 'category_name': 'Contribution', 'priority': 0, 
'category': 13, 'name': 'Pag-Ibig'}", "{'id': 13, 'category_name': 
'Contribution', 'priority': 0, 'category': 13, 'name': 'SSS'}" ];
let mapping= data.map((tmp)=>{
   tmp=JSON.parse(tmp)
   return {name:tmp.name,priority:tmp.priority}
})
Jatin Parmar
  • 2,759
  • 5
  • 20
  • 31