Skip to Content
go-dicom(3)
Packages

jsonrep

A JSON form of its own: a fixed struct of about twenty common attributes (DicomDataset), with helpers to serialize, filter and merge it, and a handler that moves bulk data out to files or URIs.

import "github.com/amrshadid/go-dicom/jsonrep" · pkg.go.dev

When to use it

Only if you want exactly this flat summary shape. It is not the DICOM JSON Model of PS3.18 Annex F, and it does not read or write a data set: you copy the attributes into the struct yourself.

For DICOM JSON, the form DICOMweb and other tools read, use dataset.Dataset.ToDICOMJSON and FromDICOMJSON, or attributes.DICOMJSON in filereader/attributes to write a header without linking the data set.

Key types and functions

NameWhat it is for
NewJSONRepresentation, JSONRepresentationThe serializer.
DicomDatasetThe fixed struct: patient, study, series and instance identifiers, dates, a few numbers, and CustomAttributes.
JSONRepresentation.ToJSON, JSONRepresentation.FromJSONStruct to JSON and back.
JSONRepresentation.FilterDataset, JSONRepresentation.MergeDatasetsKeep some fields; merge two.
JSONRepresentation.ExtractUIDs, JSONRepresentation.ExtractPatientInfoFields as maps.
NewBulkDataHandler, BulkDataHandlerKeep large values in files or memory and refer to them by URI.
NewJSONEncoderWithBulkEncode with values above a size moved to bulk data.
DetermineVRA VR guessed from a field name.

Example

// The DICOM JSON Model of a file, by the data set's own writer, which is what
// to use in place of this package.
package main

import (
	"fmt"
	"log"
	"os"

	"github.com/amrshadid/go-dicom/filebase"
	"github.com/amrshadid/go-dicom/filereader"
	"github.com/amrshadid/go-dicom/jsonrep"
)

func main() {
	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()
	df, err := filereader.ReadDICOMFile(filebase.NewFileReader(f))
	if err != nil {
		log.Fatal(err)
	}
	ds := df.GetDataset()

	// PS3.18 Annex F: keyed by tag, with vr and Value.
	model, err := ds.ToDICOMJSON()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("DICOM JSON: %d attributes, 00100010 = %+v\n", len(model), model["00100010"])

	// This package's own form: a fixed struct of common attributes.
	jr := jsonrep.NewJSONRepresentation()
	out, err := jr.ToJSON(&jsonrep.DicomDataset{
		PatientName: ds.GetStringByKeyword("PatientName"),
		PatientID:   ds.GetStringByKeyword("PatientID"),
		Modality:    ds.GetStringByKeyword("Modality"),
	})
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("jsonrep:", string(out))
}

On CT_small.dcm the first line shows the DICOM JSON Model, 258 attributes with 00100010 as {VR:PN Value:[map[Alphabetic:CompressedSamples^CT1]] ...}; then this package's flat object with three fields.

Notes

  • No other package in the module uses jsonrep.
  • DicomDataset holds no VRs, no sequences beyond ReferencedSOPSequence, and no pixel data, so it does not round-trip a file.

Note

Prefer dataset.Dataset.ToDICOMJSON / FromDICOMJSON for JSON that other DICOM software can read.

See also

dataset · filereader/attributes · dicomweb · Reading and writing

On this page