I successfully converted an excel spreadsheet into a JSON object by using the xml2js parseString() method, followed by JSON.stringify to format the result.
After reviewing the data in the console, it appears that I should be able to easily iterate through it to display the fields of interest.
Despite my efforts, I have encountered difficulties in iterating through the file, "Cell" by "Cell" using various forms of v-for.
Any guidance or direction towards a solution would be greatly appreciated.
Below is the code snippet:
<template>
<q-page class="flex flex-center">
<div>
<div v-for="(jsonObject, i) in json" :key="i">
{{ jsonObject }}
</div>
</div>
</q-page>
</template>
<script>
import { parseString } from 'xml2js'
// import xml2js from 'xml2js'
export default {
name: 'PageIndex',
data() {
return {
xmlObj: [],
jsonObj: [],
json: []
}
},
methods: {
convertXmltoJs() {
this.$axios(`saundz_curriculum.xml`).then(response => {
// console.log('response.data:', response.data)
parseString(response.data, (err, result) => {
if(err) {
console.log('err:', err)
} else {
this.jsonObj = result
// JSON object with XML data
// console.log('jsonObj:', this.jsonObj)
// stringify JSON object
this.json = JSON.stringify(result, null, 4);
// log JSON object stringified
console.log('json:', this.json)
}
})
})
}
}
}
</script>
The console output displays the following:
json: {
"Table": {
"Row": [
...
]
}
}
}
Upon examining the object before JSON.stringify, I can see the desired data but struggle with accessing it. Here is what appears in the console log without stringify:
When I console.log the result before stringify, I am able to see the data I need but face challenges in extracting it.
jsonObj:
-- (obj: Observer)
......Table: Object
........Row: Array (5)
..........0:
............Cell: (...)
.............__ob__: Observer
...............dep: Dep {id: 2260, subs: Array(0)}
................value:
..................Cell: Array(11)
.....................0:
.......................Data: Array(1)
.........................0: "CHAPTER"
This helps me identify that the target data I'm looking for is within the "CHAPTER" field.