Ali Allam
Ali Allam

Reputation: 45

Create a new JSON array that contain specific properties from another JSON

I have this JSON array of objects

  "questions": [
  {
    "id": "id",
    "answer":"answer",
    "question_type": "q_type",
    "question": {
      "header":"Text",
      "question_image": "link", 
      "answers_list": ["array"],
    }
  },
  {
    "id": "id",
    "answer":"answer",
    "question_type": "q_type",
    "question": {
      "header": "Text",
      "question_image": "link",
      "choices_type": "text",
      "choices": ["array"] 
    }
  }
 ]

and i want to extract from this json another json that contain id and answer only like this

"answers": [{"question": question_id, "answer": "text"}]

Upvotes: 1

Views: 815

Answers (5)

Xotic750
Xotic750

Reputation: 23482

This will map your original JSON to the specified JSON. Uses a reviver function.

var json = '{"questions":[{"id":"id","answer":"answer","question_type":"q_type","question":{"header":"Text","question_image":"link","answers_list":["array"]}},{"id":"id","answer":"answer","question_type":"q_type","question":{"header":"Text","question_image":"link","choices_type":"text","choices":["array"]}}]}';

var keep = ['', 'questions', 'id', 'answer'];
var newJson = JSON.stringify(JSON.parse(json, function(k, v) {
  var idx = k && k == +k;
  if (idx || keep.indexOf(k) > -1) {
    if (idx && typeof v === 'object') {
      var id = v.id;
      delete v.id;
      v.question = id;
    }
    return v;
  }
}));
document.body.textContent = newJson;
console.log(newJson);

Upvotes: 0

Norbert
Norbert

Reputation: 663

var answers = questions.reduce(function (result, question){
    return result.concat(item.question.answers_list.map(function (answer) {
        return {
            question: {
                id: question.id
            },
            answer: answer
        }
    }));
}, []);

You'll need to 'map' question's data, because you'll need to be careful of circular references if you want to parse it to JSON. But you specify all you want is the question.id. So that's no problem.

Upvotes: 0

LucaLumetti
LucaLumetti

Reputation: 340

Here your json

var json = {"questions": [
  {
    "id": "id",
    "answe":"answer",
    "question_type": "q_type",
    "question": {
      "header":"Text",
      "question_image": "link", 
      "answers_list": ["array"]
    }
  },
  {
    "id": "id",
    "answer":"answer",
    "question_type": "q_type",
    "question": {
      "header": "Text",
      "question_image": "link",
      "choices_type": "text",
      "choices": ["array"] 
    }
  }
 ]}

Now iterate it and create a new Json object

var answers = []
var question = {}

json.questions.forEach(function(value, index){
    question.id = value.id;
    question.answer = value.answer;
    answers.push(question);

});

var resultJson = {};

resultJson.answers = answers;

Upvotes: 0

torbenl
torbenl

Reputation: 296

You can use the JavaScript map function (assuming the variable q contains your questions object):

var out = q.questions.map(function(element) {
              return {"question": element.id, "answer": element.answer};
          });

Upvotes: 4

raduation
raduation

Reputation: 792

Assuming both the question id and the answer are in the questions objects, then you could do it as follows:

var answers = [];

for(var i = 0; i < questions.length; i++)
{
    answers.push({
        question: questions[i].id,
        answer: questions[i].answer
    });
}

This also assumes you declare the questions array as var questions = ...

Upvotes: 0

Related Questions