How to Convert a DICOM File to JSON using DicomJsonSerializer
This tutorial demonstrates how to convert DICOM files to JSON format using C#. JSON output is essential for integrating DICOM metadata with modern web services, REST APIs, and healthcare information systems.
Benefits of Converting DICOM to JSON
- Web Integration:
- JSON is the standard format for REST APIs and web services.
- Data Analysis:
- Easily process DICOM metadata in data analytics pipelines.
- Interoperability:
- Share metadata with systems that don’t support native DICOM formats.
Prerequisites: Preparing the Environment
- Set up Visual Studio or any compatible .NET IDE.
- Create a new .NET 8 console application project.
- Install Aspose.Medical from the NuGet Package Manager.
Step-by-Step Guide to Convert DICOM to JSON
Step 1: Install Aspose.Medical
Add the Aspose.Medical library to your project using NuGet.
Install-Package Aspose.MedicalStep 2: Include Necessary Namespaces
Add references to the required namespaces in your code.
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;Step 3: Load the DICOM File
Load the DICOM file you want to convert.
DicomFile dcm = DicomFile.Open("patient_scan.dcm");Step 4: Serialize to JSON
Use the DicomJsonSerializer.Serialize method to convert the DICOM file to JSON.
string json = DicomJsonSerializer.Serialize(dcm);Step 5: Save or Use the JSON Output
Save the JSON to a file or use it directly in your application.
// Save to file
File.WriteAllText("patient_scan.json", json);
// Or use directly
Console.WriteLine(json);Complete Code Example to Convert DICOM to JSON
Here is a complete example demonstrating how to convert a DICOM file to JSON:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
// Load the DICOM file
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Convert to JSON
string json = DicomJsonSerializer.Serialize(dcm);
// Save to file
File.WriteAllText("patient_scan.json", json);
Console.WriteLine("DICOM file converted to JSON successfully!");
Console.WriteLine($"Output saved to: patient_scan.json");Pretty-Printed JSON Output
For human-readable JSON with indentation, use the writeIndented parameter:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Serialize with indentation for readability
string prettyJson = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
File.WriteAllText("patient_scan_pretty.json", prettyJson);
Console.WriteLine("Pretty-printed JSON saved successfully!");Converting Dataset Instead of DicomFile
You can also convert just the Dataset portion:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
// Serialize only the dataset (without file meta information)
string datasetJson = DicomJsonSerializer.Serialize(dcm.Dataset);
File.WriteAllText("patient_dataset.json", datasetJson);Example JSON Output Structure
The JSON output follows the DICOM PS3.18 standard. Here’s an example of what the output looks like:
{
"00080005": {
"vr": "CS",
"Value": ["ISO_IR 100"]
},
"00080020": {
"vr": "DA",
"Value": ["20240115"]
},
"00080030": {
"vr": "TM",
"Value": ["143022"]
},
"00100010": {
"vr": "PN",
"Value": [
{
"Alphabetic": "DOE^JOHN"
}
]
},
"00100020": {
"vr": "LO",
"Value": ["12345"]
}
}Stream-Based Serialization
For large files or web applications, use stream-based serialization:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
DicomFile dcm = DicomFile.Open("large_scan.dcm");
// Write directly to a file stream
using (FileStream fs = File.Create("large_scan.json"))
{
DicomJsonSerializer.Serialize(fs, dcm.Dataset);
}
Console.WriteLine("Large DICOM file serialized to JSON stream!");Integration Example: Sending JSON to Web API
Here’s how to integrate DICOM to JSON conversion with an HTTP client:
using Aspose.Medical.Dicom;
using Aspose.Medical.Dicom.Serialization;
using System.Net.Http;
using System.Text;
DicomFile dcm = DicomFile.Open("patient_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm);
// Send to web API
using HttpClient client = new();
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(
"https://api.example.com/dicom/metadata",
content
);
if (response.IsSuccessStatusCode)
{
Console.WriteLine("DICOM metadata successfully sent to API!");
}Usage Tips
Debugging DICOM Metadata
Use JSON conversion to inspect DICOM tag values:
DicomFile dcm = DicomFile.Open("unknown_scan.dcm");
string json = DicomJsonSerializer.Serialize(dcm, writeIndented: true);
Console.WriteLine(json);Sending to Web Front-End
JSON is ideal for displaying DICOM metadata in browser-based viewers:
// In ASP.NET Core controller
[HttpGet("dicom/{id}/metadata")]
public IActionResult GetMetadata(string id)
{
DicomFile dcm = DicomFile.Open($"storage/{id}.dcm");
string json = DicomJsonSerializer.Serialize(dcm);
return Content(json, "application/json");
}What’s Included in JSON Output
The JSON output includes:
- All DICOM tags with their values
- Value Representations (VR) for each tag
- Sequence items as nested JSON objects
- Binary data references (BulkData) for pixel data
Additional Information
- The JSON format follows DICOM PS3.18 Web Services specification.
- Large binary values (like pixel data) are typically referenced rather than embedded.
- Consider using custom serialization options for specific integration requirements.
Conclusion
This tutorial has shown you how to convert DICOM files to JSON format in C# using Aspose.Medical. JSON output enables seamless integration with modern healthcare APIs, web viewers, and data analytics systems.