-3

After scraping an html table I have stored my data in the variable:

var data = ["header1", "header2", "a1", "a2", "b1", "b2", "c1", "c2"];

I want to create a json file in the form:

var json = [
  {
    "header1":"a1", 
    "header2":"a2"
  },
  {
    "header1":"b1", 
    "header2":"b2"
  },
  {
    "header1":"c1", 
    "header2":"c2"
  }
]

I know that I need

var headers={}; 

and load it with

headers["header1"]
headers["header2"]

I also need

var jsonObj = []; 

and push the array headers inside jsonObj:

jsonObj.push(headers);

Is it possible to create it?

Sebastian
  • 1,321
  • 9
  • 21
  • There's no such thing as a "JSON array" (or ["JSON object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/)). `json` is an array of objects. There's no [JSON](https://www.json.org/json-en.html) at all in your question. – Andreas Oct 19 '21 at 09:54
  • [What is the difference between JSON and Object Literal Notation?](https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) – Andreas Oct 19 '21 at 09:54
  • I have a large number of data in my real code and I am trying to understand how to loop in the array of strings and "isolate" the headers.. – sqlQueryAll Oct 19 '21 at 09:58
  • How can you distinguish headers from data? – jabaa Oct 19 '21 at 09:59
  • I know headers are data[0] and data[1] – sqlQueryAll Oct 19 '21 at 10:00
  • It's always two columns but unspecified rows? – jabaa Oct 19 '21 at 10:01
  • yes thre are always two columns – sqlQueryAll Oct 19 '21 at 10:02
  • How do you differentiate between which value going to header1 and which one to header2? –  Oct 19 '21 at 10:03
  • It is always like that.. First two elements are my headers and then data[2], data[4] .. go to header1 and data[3], data[5]... go to header2 – sqlQueryAll Oct 19 '21 at 10:06

2 Answers2

1

I'll make some assumptions, and then attempt to answer your question.

To me, it seems your table looks something like:

header1 header2
a1 a2
b1 b2
c1 c2

Simply starting with the array you already have, we can then just pick out two and two values at the time. See the code below

const COLUMN_COUNT = 2
const data = ["header1", "header2", "a1", "a2", "b1", "b2", "c1", "c2"]

// this removes the headers from the data-list, 
// and stores them in the headers variable
const headers = data.splice(0, COLUMN_COUNT)
const output = []

while(data.length > 0) {
  const row = {}
  const values = data.splice(0, COLUMN_COUNT)
  for(const [idx, value] of values.entries()) {
    const header = headers[idx]
    row[header] = value
  }
  output.push(row)
}

console.log(output)
Sebastian
  • 1,321
  • 9
  • 21
0

There is actually no relation between header and the values, but assuming the first two elements are always header1 and header2 and the next 1 pair will be the value of header1 and header2 respectively, this might do it

const data = ["header1", "header2", "a1", "a2", "b1", "b2", "c1", "c2"];
const res = [];
const h1 = data[0];
const h2 = data[1];
data.splice(0, 2);


for (let i = 0; i < data.length; i++) {
    if (i % 2 != 0) {
        const payload = {};
        payload[h1] = data[i - 1];
        payload[h2] = data[i];
        res.push(payload);
    }
}

console.log(res)
AnanthDev
  • 1,605
  • 1
  • 4
  • 14