将带有嵌套对象的JSON转换为CSV不适用于常见解决方案

时间:2019-03-07 19:17:35

标签: javascript json csv

我尝试使用this通用解决方案,在Stack Overflow和Internet上都发现了使用Javascript将JSON转换为CSV的方法,但是没有得到正确的结果。到目前为止,还没有其他解决方案能起作用,因为它们无法处理嵌套对象。

我认为我的问题是我的JSON具有嵌套对象的数组,但是我一直找不到任何解决方案。

这是我的JSON示例:

[
  {
    "Id": "1",
    "Course": "101",
    "Student": "101",
    "quizzes": [
      {
        "Id": "1",
        "name__c": "Quiz 1",
        "grade__c": 100,
        "questions": [
          {
            "Id": "1",
            "question__c": "Question 1",
            "answer__c": "Answer 1"
          },
          {
            "Id": "2",
            "question__c": "Question 2",
            "answer__c": "Answer 2"
          }
        ]
      },
      {
        "Id": "2",
        "name__c": "Quiz 2",
        "grade__c": 100,
        "questions": [
          {
            "Id": "1",
            "question__c": "Question 1",
            "answer__c": "Answer 1"
          },
          {
            "Id": "2",
            "question__c": "Question 2",
            "answer__c": "Answer 2"
          }
        ]
      }
    ]
  }
]

这是我尝试的解决方案:

var jsonString = '[{"Id":"1","Course":"101","Student":"101","quizzes":[{"Id":"1","name__c":"Quiz 1","grade__c":100,"questions":[{"Id":"1","question__c":"Question 1","answer__c":"Answer 1"},{"Id":"2","question__c":"Question 2","answer__c":"Answer 2"}]},{"Id":"2","name__c":"Quiz 2","grade__c":100,"questions":[{"Id":"1","question__c":"Question 1","answer__c":"Answer 1"},{"Id":"2","question__c":"Question 2","answer__c":"Answer 2"}]}]}]';
var array = typeof jsonString != 'object' ? JSON.parse(jsonString) : jsonString;
var str = '';
for (var i = 0; i < array.length; i++) {
    var line = '';
    for (var index in array[i]) {
        if (line != '') {
            line += ',';
        }
        line += array[i][index];
    }
    str += line + '\r\n';
}
console.log(str);

但是,我的控制台正在打印:

1,101,101,[object Object],[object Object]

何时应该打印出来:

"Id","Course","Student","quizzes__Id","quizzes__name__c","quizzes__grade__c","quizzes__questions__Id","quizzes__questions__question__c","quizzes__questions__answer__c"
"1","101","101","1","Quiz 1","100","1","Question 1","Answer 1"
"","","","","","","2","Question 2","Answer 2"
"","","","2","Quiz 2","100","1","Question 1","Answer 1"
"","","","","","","2","Question 2","Answer 2"

我使用了相同的JSON,并使用了this online converter,它会打印出我需要的结果。

我是否缺少处理嵌套对象的额外步骤?

1 个答案:

答案 0 :(得分:0)

这将遍历所有学生课程,测验和问题。 手动添加您喜欢的列:

const jsonData = [
  {
    "Id": "1",
    "Course": "101",
    "Student": "101",
    "quizzes": [
      {
        "Id": "1",
        "name__c": "Quiz 1",
        "grade__c": 100,
        "questions": [
          {
            "Id": "1",
            "question__c": "Question 1",
            "answer__c": "Answer 1"
          },
          {
            "Id": "2",
            "question__c": "Question 2",
            "answer__c": "Answer 2"
          }
        ]
      },
      {
        "Id": "2",
        "name__c": "Quiz 2",
        "grade__c": 100,
        "questions": [
          {
            "Id": "1",
            "question__c": "Question 1",
            "answer__c": "Answer 1"
          },
          {
            "Id": "2",
            "question__c": "Question 2",
            "answer__c": "Answer 2"
          }
        ]
      }
    ]
  }
]

const columnNames = ['courseId', 'studentId', 'quizId', 'questionId']
const mapToCsvRow = (items = []) => {
  return items.map((x) => `"${x}"`).join(', ')
}
const csvOutputRows = [
  mapToCsvRow(columnNames) // write column header
]

jsonData.forEach((courseData) => {
  courseData.quizzes.forEach((quizData) => {
    quizData.questions.forEach((question) => {
      csvOutputRows.push(mapToCsvRow([
        courseData.Course,
        courseData.Student,
        quizData.Id,
        question.Id,
      ]))
    })
  })
})

const csv = csvOutputRows.join("\n")