简体   繁体   中英

Why are some fields in JSON output missing quotes and why no double quotes in output

Below is screenshot of vs code terminal output. The JSON output is treating the 3 fields with red arrows differently than the others. The string has double quotes around easch item but tje JSOn.parse output has single quotes' For test purposes, code is set to handle 1 listing.

在此处输入图像描述

Here is code

 const request = require('request-promise'); const cheerio = require('cheerio'); const fs = require('fs'); const { JSDOM } = require("jsdom"); fs.readFile('./data/hb2019.html', 'utf-8', (err, data) => { if (err) { console.log(err); return; } const dom = new JSDOM(data); fieldName = []; fieldName = [...dom.window.document.querySelectorAll('#tableComparableSales >tbody.salesReportSortLink')] //file may artificially have multiple pages var listings = [];// array of raw listing links listings = dom.window.document.querySelectorAll('[id^=tdSales]'); //var output = '['; var output = ''; (function () { field = []; //console.log(listings.length) var numListings = 1;//28 var cleanedField; for (var i = 0; i < numListings; i++) { output = output + '{'; for (var j = 1; j < 16; j++) { field[i] = dom.window.document.querySelector(`#tdSales${i + 1}>td:nth-child(${j})`); //some fields have quotes in them ie> Financial inst or "In Lieu of Forclosure" stated" cleanedField = field[i].textContent.trim().replace(/\"/g, ""); output = output + `"${fieldName[j - 1].textContent.trim()}":"${cleanedField}"`; output = output.replace(/\s+/g, ' '); // don't output last comma if (j < 15) { output = `${output},`; }; }; // don't output last comma if (i < numListings - 1) { output = output + '},'; } else { output = output + '}'; } } //output = output + ']'; output = output.replace(/\s+/g, ' '); console.log(output); var obj = JSON.parse(output); console.log(obj); })(); });

Seems issue with this line "${fieldName[j - 1].textContent.trim()}":"${cleanedField}" . Since it is a template literals so " is not required

It is not treating them differently, it is just showing them differently.

It is using quotes whenever a key contains characters not valid in Javascript variable names, eg space or # .

The same way that you can write in Javascript obj.var1 or obj['var1'] but you cannot write obj.var2% , you have to write obj['var2%'] .

BTW, this output is a text representation of a javascript object, but it is not JSON. In JSON, all object keys and strings are enclosed in double quotes " . The line above is proper JSON.

The problem was that I was copying the output string from the vsCode terminal and pasting iinto the validator (as plain text) Apparently that introduces line breaks. So I saved the string to a file and then uploade4 the file to the json validator and it validated perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM