0

So I'm having this issue where I'm trying to convert something such as

[0]['question']: "what is 2+2",
[0]['answers'][0]: "21",
[0]['answers'][1]: "312",
[0]['answers'][2]: "4"

into an actual formated json object like so

[
  {
    'question': 'what is 2+2',
    'answers': ["21", "312", "4"]
  }
]

but I'm not too sure what approach to take to make this work.

I'm planning on parsing the key-values in the first snipped through javascript and decode it into a json object like in the second snippet through python.

Have you got any idea on how to do this? I'd accept an example in pretty much any language as it shouldn't be much of a worry to read the concept behind them.

4
  • 1 - That second object isn't JSON. Do you actually mean JSON or do you just mean a javascript object that looks like that? Commented Mar 18, 2015 at 14:18
  • 2 - How do you know that 'question' is has a single string object but 'answers' is an array? Commented Mar 18, 2015 at 14:18
  • I'm sure that it'll be following this format since I'll be the one allowing input. They can only provide single strings questions. What I meant is some kind of dataset that python could parse based upon the same linked map. Commented Mar 18, 2015 at 14:22
  • So you have a model of the destination format of the data. I suggest you write a function that knows how to build the model and then feed it input line by line. Building a generic function that takes a schema and input is possible but will waste effort. If your format is lines like [0, "question", "xyz"] and [0, "answers", 0, "21"]], think how you'd add this to an empty object, including building up the various paths through the object. That's a good start. Commented Mar 18, 2015 at 14:23

2 Answers 2

1

Something like this. You need to handle input errors.

A function to take a data structure and add stuff to it based on input

function add(old, input) {
  var index = input[0];
  var section = input[1];
  if (old[index] == undefined) {
    old[index] = {}
  };
  if (section == "question") {
    old[index]['question'] = input[2];
  }

  if (section == "answers") {
    var answerIndex = input[2];
    var answerValue = input[3];

    if (old[index]["answers"] == undefined) {
      old[index]["answers"] = []
    };

    old[index]["answers"][answerIndex] = answerValue
  }

  return old;
}

Some inputs:

var inputs = [[0, "question", "what"],
              [0, "answers", 0, "21"],
              [0, "answers", 1, "22"]];

var result = {};

inputs.forEach(function(input) { add(result, input) })

JSON.stringify(result)


"{"0":{"question":"what","answers":["21","22"]}}"
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should format the json as follow:

{
    "questions": [
        {
            "question": "What is 2+2",
            "possible_answers": [
                {
                    "value": 1,
                    "correct": false
                },
                {
                    "value": 4,
                    "correct": true
                },
                {
                    "value": 3,
                    "correct": false
                }
            ]
        },
        {
            "question": "What is 5+5",
            "possible_answers": [
                {
                    "value": 6,
                    "correct": false
                },
                {
                    "value": 7,
                    "correct": false
                },
                {
                    "value": 10,
                    "correct": true
                }
            ]
        }
    ]
}

for doing that, you can do it:

var result = {}
result.questions = []; //the questions collection

var question = {};    //the first question object
question.question = "what is 2 + 2";      
question.possible_answers = [];

var answer1 = {};
answer1.value = 1;
answer1.correct = false;

var answer2 = {};
answer2.value = 2;
answer2.correct = true;

var answer3 = {};
answer3.value = 3;
answer3.correct = false;

question.possible_answers.push(answer1);
question.possible_answers.push(answer2);
question.possible_answers.push(answer3);

result.questions.push(question); //add the first question with its possible answer to the result.

You can help yourself using jsonlint for formatting the json and then try to set your javascript object to get the json you want.

Hope helps you!

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.