Search code examples
javascriptnode.jsdicomdcmtk

DCMTK dcm2json produces invalid JSON for broken DS or IS values


DCMTK dcm2json produces invalid JSON for broken DS or IS values see https://support.dcmtk.org/redmine/issues/769

The JSON will contain values like this example:

"00291003": {"vr":"IS","Value":[F]},

Where 'F' is clearly not a number.


Solution

  • This can be worked around in a Nodejs environment by first running the raw unparsed JSON through the following.

    const jsonRepair = (rawJson) => {
        let regex1 =  /\[([^"]+)\]/g
        let isNumRegex = /^[-.0-9]*\d$/g
        let matches = undefined
        while ((matches = regex1.exec(rawJson)) !== null) {
            if (!isNumRegex.test(matches[1])) {
                rawJson = rawJson.replace(matches[0], `["${matches[1]}"]`)
                console.log(matches[0], `[${matches[1]}]`)
            }
        }
        return rawJson
    }