I have a snippet of javascript code that I'm utilizing with casperjs to iterate through links and retrieve data in json format.
Below is the code snippet:
casper.each(links, function (self, link) {
this.thenOpen(link, function () {
// obtain work order information
var town_selector = 'div tr';
var town_names_info = this.getElementsInfo(town_selector);
var town_names = [];
for (var i = 0; i < town_names_info.length; i++) {
town_names.push(town_names_info[i].text.trim().replace(/\n\s+\n/, ''));
}
var jsonTest = arrayToObject(town_names);
json.push(JSON.stringify(jsonTest));
casper.capture('./images/workOrder' + workOrder + '.png');
workOrder++
utils.dump(jsonTest);
array.push(jsonTest);
casper.thenClick(x('/html/body/table[2]/tbody/tr/td[2]/a[2]'), function () {
// carry out some additional tasks here
someLinks = this.evaluate(getLinks);
for (var i = 0; i < someLinks.length; i++) {
someLinks[i] = "https://somelink" + someLinks[i];
}
casper.each(someLinks, function (self, link) {
self.thenOpen(link, function () {
var selector = 'div tr';
var names_info = this.getElementsInfo(town_selector);
var names = [];
for (var i = 0; i < names_info.length; i++) {
names.push(names_info[i].text.trim().replace(/\n\s+\n/, ''));
}
var jsonTest = arrayToObject(names);
json.push(JSON.stringify(jsonTest));
utils.dump(jsonTest);
array.push(jsonTest);
fs.write('results.json', JSON.stringify(array), 'w');
casper.capture('./images/lineItem' + lineItem + '.png');
lineItem++
});
});
});
});
});
The following shows the output for two activities each with two line items:
[{"Activity #":"some activity",
"Customer":"some customer",
"Account #":"some account"},
{
"Line #":"1",
"Action Required":"",
"Status":"some status",
"Product Line":"some product line",
"Product":"some product"},
{
"Line #":"2",
"Action Required":"",
"Status":"some status",
"Product Line":"some product line",
"Product":"some product"},
{
"Activity #":"some other activity",
"Customer":"some other customer",
"Account #":"some other account"},
{
"Line #":"1",
"Action Required":"",
"Status":"some status",
"Product Line":"some product line",
"Product":"some product"},
{
"Line #":"2",
"Action Required":"",
"Status":"some status",
"Product Line":"some product line",
"Product":"some product"}]
Please assist me in transforming my output to resemble the following structure:
[{
"Activity #": "some activity",
"Customer": "some customer",
"Account #": "some account",
"lineItems": [
{
"Line #": "1",
"Action Required": "",
"Status": "some status",
"Product Line": "some product line",
"Product": "some product"
},
{
"Line #": "2",
"Action Required": "",
"Status": "some status",
"Product Line": "some product line",
"Product": "some product"
}
]
},
{
"Activity #": "some activity",
"Customer": "some customer",
"Account #": "some account",
"lineItems": [
{
"Line #": "1",
"Action Required": "",
"Status": "some status",
"Product Line": "some product line",
"Product": "some product"
},
{
"Line #": "2",
"Action Required": "",
"Status": "some status",
"Product Line": "some product line",
"Product": "some product"
}
]
}]
Thank you for your assistance.