0

Could anyone give me a clue what is wrong with the JSON syntax listed below. According to JsonLint the error is/begins in front of the word "csv". I cannot see the error in the syntax but it must be there. If someone could just tell me the principle behind my error please.

{
"lists": {
    "csv": "function(head, req) {
    var row,
        first = true;

    // output HTTP headers
    start({
        headers: {
            'Content-Type': 'text/csv'
        },
    });

    // iterate through the result set
    while (row = getRow()) {

        // get the doc (include_docs=true)
        var doc = row.doc;

        // if this is the first row
        if (first) {

            // output column headers
            send(Object.keys(doc).join(',') + 'n');
            first = false;
        }

        // build up a line of output
        var line = '';

        // iterate through each row
        for (var i in doc) {

            // comma separator
            if (line.length > 0) {
                line += ',';
            }

            // output the value, ensuring values that themselves
            // contain commas are enclosed in double quotes
            var val = doc[i];
            if (typeof val == 'string' && val.indexOf(',') > -1) {
                line += '" ' + val.replace(/"/g, ' "" ') + ' "';
            } else {
                line += val;
            }
        }
        line += 'n';

        // send  the line
        send(line);
    }
}
"
}
}

EDIT:

Full code(CouchDB view/list):

{
"_id": "_design/comptno",
"_rev": "2-4531ba9fd5bcd6b7fbc5bc8555f0bfe3",
"views": {
"list_example": {
  "map": "function(doc) {\r\n  if (doc.compartment.number) {\r\n      emit(doc.compartment.number, null);\r\n  }\r\n};"
 },
"list_example2": {
  "map": "function(doc) {\r\n  if (doc.compartment.number) {\r\n    emit(doc.compartment.number, null);\r\n  }\r\n};"
}
},
"lists":{"csv":"function(head, req) {    var row,        first = true;    // output HTTP headers    start({        headers: {            'Content-Type': 'text/csv'        },    });    // iterate through the result set    while (row = getRow()) {        // get the doc (include_docs=true)        var doc = row.doc;        // if this is the first row        if (first) {            // output column headers            send(Object.keys(doc).join(',') + 'n');            first = false;        }        // build up a line of output        var line = '';        // iterate through each row        for (var i in doc) {            // comma separator            if (line.length > 0) {                line += ',';            }            // output the value, ensuring values that themselves            // contain commas are enclosed in double quotes            var val = doc[i];            if (typeof val == 'string' && val.indexOf(',') > -1) {                line += '"' + val.replace(/"/g, '""') + '"';            } else {                line += val;            }        }        line += 'n';        // send  the line        send(line);    }}"},
"language": "javascript"
}
jlb333333
  • 371
  • 2
  • 13
  • You can't have line breaks in string values. – JJJ Feb 04 '17 at 08:26
  • Possible duplicate of [Invalid Json Error in JsonLint](http://stackoverflow.com/questions/21675038/invalid-json-error-in-jsonlint) – JJJ Feb 04 '17 at 08:27
  • @JJJ Thanks for the comments. I have added an edit with the minified json, including the whole document for an overview. I have used http://codebeautify.org/jsonvalidator as a double-check. JsonLint and CodeBeautify both point now to this region as erroneous: ' line += '"' + val.replace(/"/g, '""') + ' ' The code was posted on the Cloudant website and I have commented there. [link](https://developer.ibm.com/clouddataservices/2015/09/22/export-cloudant-json-as-csv-rss-or-ical/) – jlb333333 Feb 04 '17 at 14:47
  • You need to escape the double quotes. – JJJ Feb 04 '17 at 15:11
  • @JJJ Escaping the double quotes did it.Thank you. – jlb333333 Feb 04 '17 at 18:26

1 Answers1

-2

Here corrected without multilines:

{
  "lists": {
    "csv": [
      "function(head, req) {",
      "var row,",
      "first = true;",
      "",
      "// output HTTP headers",
      "start({",
      "headers: {",
      "'Content-Type': 'text/csv'",
      "},",
      "});",
      "",
      "// iterate through the result set",
      "while (row = getRow()) {",
      "// get the doc (include_docs=true)",
      "var doc = row.doc;",
      "// if this is the first row",
      "if (first) {",
      "// output column headers",
      "send(Object.keys(doc).join(',') + 'n');",
      "first = false;",
      "}",
      "// build up a line of output",
      "var line = '';",
      "// iterate through each row",
      "for (var i in doc) {",
      "// comma separator",
      "if (line.length > 0) {",
      "line += ',';",
      "}",
      "// output the value, ensuring values that themselves",
      "// contain commas are enclosed in double quotes",
      "var val = doc[i];",
      "if (typeof val == 'string' && val.indexOf(',') > -1) {",
      "line += '\" ' + val.replace(/\"/g, ' \"\" ') + ' ';",
      "} else {",
      "line += val;",
      "}",
      "}",
      "line += 'n';",
      "// send  the line",
      "send(line);",
      "}",
      "}",
      ""
    ]
  }
}
BladeMight
  • 2,670
  • 2
  • 21
  • 35
  • Why downvoted, I guess this is the only *readable* way of multi-line json, here: http://stackoverflow.com/questions/2392766/multiline-strings-in-json it is used as well. – BladeMight Feb 04 '17 at 11:57
  • 2
    Doesn't this modification change the meaning of the json completely? – jlb333333 Feb 04 '17 at 14:40
  • @jlb333333 maybe so, but there are no multi-line in JSON anyway... But if you read answers/comments in topic I suggested, you may understand why I suggested it. – BladeMight Feb 04 '17 at 15:15