larsgw / citation.js

Citation.js converts formats like BibTeX, Wikidata JSON and ContentMine JSON to CSL-JSON to convert to other formats like APA, Vancouver and back to BibTeX.
https://citation.js.org/
MIT License
219 stars 30 forks source link

Extra comma producing invalid JSON output #143

Closed Galadirith closed 6 years ago

Galadirith commented 6 years ago
  1. What version of Citation.js are you using? citation-js@v0.4.0-1

  2. What version of Node and what OS are you using? node@v10.1.0 and debian@jessie through the official docker node image running on OS X 10.11.6 (El Capitan)

    docker pull node:10.1.0
  3. What did you do? (this includes input data, configuration, etc.)

    # refs.bib
    @article{Creighton:2000gu,
          author         = "Creighton, Teviet",
          title          = "{Tumbleweeds and airborne gravitational noise sources for LIGO}",
          journal        = "Class. Quant. Grav.",
          volume         = "25",
          year           = "2008",
          pages          = "125011",
          doi            = "10.1088/0264-9381/25/12/125011",
          eprint         = "gr-qc/0007050",
          archivePrefix  = "arXiv",
          primaryClass   = "gr-qc",
          SLACcitation   = "%%CITATION = GR-QC/0007050;%%"
    }
    @article{Finstad:2018wid,
          author         = "Finstad, Daniel and De, Soumi and Brown, Duncan A. and Berger, Edo and Biwer, Christopher M.",
          title          = "{Measuring the viewing angle of GW170817 with electromagnetic and gravitational waves}",
          year           = "2018",
          eprint         = "1804.04179",
          archivePrefix  = "arXiv",
          primaryClass   = "astro-ph.HE",
          SLACcitation   = "%%CITATION = ARXIV:1804.04179;%%"
    }
    npm install --save-dev citation-js
    npx citation-js -i refs.bib -f json -o refs
  4. What did you expect to see? Conversion of the two bibtex entries to a valid csl-json file.

  5. What actually happened? An almost valid csl-json file, but with an extra , separating the two elements of the output json

    ...
    },
    ,{
    ...

    I expected

    ...
    },{
    ...

    the full output is below

    # refs.json
    [{
        "author": [
        {
        "given": "Teviet",
        "family": "Creighton"
    }
    ],
        "title": "Tumbleweeds and airborne gravitational noise sources for LIGO",
        "container-title": "Class. Quant. Grav.",
        "volume": "25",
        "page": "125011",
        "DOI": "10.1088/0264-9381/25/12/125011",
        "type": "article-journal",
        "citation-label": "Creighton:2000gu",
        "id": "Creighton:2000gu",
        "year-suffix": "gu",
        "issued": {
        "date-parts": [
        [
        2008
    ]
    ]
    }
    },
    ,{
        "author": [
        {
        "given": "Daniel",
        "family": "Finstad"
    },
        {
        "given": "Soumi",
        "family": "De"
    },
        {
        "given": "Duncan A.",
        "family": "Brown"
    },
        {
        "given": "Edo",
        "family": "Berger"
    },
        {
        "given": "Christopher M.",
        "family": "Biwer"
    }
    ],
        "title": "Measuring the viewing angle of GW170817 with electromagnetic and gravitational waves",
        "type": "article-journal",
        "citation-label": "Finstad:2018wid",
        "id": "Finstad:2018wid",
        "year-suffix": "wid",
        "issued": {
        "date-parts": [
        [
        2018
    ]
    ]
    }
    }]

    Attempting to import this via JSON.parse(...) fails. If you have more bibtex entries you get an extra comma between every paid of output items. For a single bibtex entry you get valid json output.

Thanks so much for an amazing package @larsgw :blush: 👍

larsgw commented 6 years ago

Thanks for the bug report! I'm glad you find the package useful.

I made my own JSON serialisation function to be able to output HTML-formatted JSON. I don't know if there's a use case for that, but I included it anyway. That was probably a bad idea on my end, mainly because I know don't support JSON (not really, that is). I'll see what causes the issue and publish a fix when I can. Until then, you could create a script that serialises the data directly, i.e.:

var Cite = require('citation-js')

var data = new Cite(input)

var output = JSON.stringify(data.data)
// optionally JSON.stringify(data.data, null, 2) for nicer formatting

//  '[{...}, {...}]'

I'll also look at replacing this functionality with something more sensible. For updates on that, see #144.

Galadirith commented 6 years ago

@larsgw Thanks so much for the reply, I really appreciate it.

The solution worked great, thanks so much for finding one so quick.

I tried a few packages to do this conversion and citation-js was by far the easiest to use, and also the most reliable convertor (with others dropping fields that should have been converted, or mangling fields etc). Even though the solution here is not on the command line, wrapping that in a script is simple enough and it's still the simplest solution :blush:

If there's a fix for this in the future that would be amazing, but I also completely appreciate that this is one of the more periphery features, so certainly no rush and if it's never fixed there is a working solution https://github.com/larsgw/citation.js/issues/143#issuecomment-388486047 👍

I'd be happy to close this in favour of #144 but I'll leave it for you in case you wanted this left open for reference.

larsgw commented 6 years ago

No problem! I added a commit that should fix this issue to master, but a new release might take some time, while I'm shifting around methods and namespaces.