Skip to main content

DCC XML<-->JSON Conversion

Thie Package contains an Libary for XML<-->JSON Conversion for the DCC (Digital Calibration Certificate). Witch contains an Python Module for the conversion. Aswell as an tornador REST-API Server to remote call this conversion.

Conversion Rules

  1. The XML Document ist converted into a nested dict, containing lists for repatable elements. Repeated elements are determined by there name or by the name of the parent Element. schemaTypeCastCache.json contains a comprehensive list of the repeated fild names/parent names.
  2. The actual string Conent of an Element is stored as string in the #text Element of the JSON-Nodes. If the content is an List of strings, the #list Element contains the list of elements vastet to float if posible.
  3. Since JSON Don't support attributes, all attributes are converted to dict Keys Starting with an @
  4. Comments are converted to an adition dict key '@_Comment' containing a list of all comments in the coresonding XML-Element, the order ist preserved. The actual position of the comment is not.
  5. when converting json to XML if an Element contains a #list key, and a '#text' key, the #text key is used and the '#list' is discarded. If only the #list key is present, the list is converted to a string and convertet.

Installation

To use the conversion libary, install the package with pip:

pip install dccXMLJSONConv

to use the REST-API server consider cloning the repo and installing the additional requirements from requirementsServer.txt

Error handling

The Converter does not check any XSD-rules and allways tries to return an response.

Conversion example

<dcc:quantity refType="vib_phase">
	<!-- This is only an example -->
	<dcc:name>
		<dcc:content lang="de">Phasenverzögerung</dcc:content>
		<dcc:content lang="en">Phase delay</dcc:content>
	</dcc:name>
	<!-- This is an additional Comment -->
	<si:realListXMLList>
		<si:valueXMLList>1.7 1.5 1.3 1.1 1.02 0.92 0.83 0.76 0.69 0.63 0.57 0.53 0.48 0.44 0.42 0.38 0.33 0.28 0.39 0.30 0.30 0.15 0.17 0.1 0.1 0.1 0.1</si:valueXMLList>
		<si:unitXMLList>\degree</si:unitXMLList>
		<si:expandedUncXMLList>
			<si:uncertaintyXMLList>1.5 1.0 1.0 1.0 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 0.70 1.0 1.0 1.0 1.0</si:uncertaintyXMLList>
			<si:coverageFactorXMLList>2.0</si:coverageFactorXMLList>
			<si:coverageProbabilityXMLList>0.95</si:coverageProbabilityXMLList>
			<si:distributionXMLList>normal</si:distributionXMLList>
		</si:expandedUncXMLList>
	</si:realListXMLList>
</dcc:quantity>
{
    "@refType": "vib_phase",
    "dcc:name": {
        "dcc:content": [
            {
                "@lang": "de",
                "#text": "Phasenverz\u00f6gerung"
            },
            {
                "@lang": "en",
                "#text": "Phase delay"
            }
        ]
    },
    "si:realListXMLList": [
        {
            "si:valueXMLList": {
                "#text": "-0.0003 -0.0004 -0.0004 -0.0003 -0.0001 -0.0002 -0.0003 -0.0001 0.0000 0.0002 -0.0002 -0.0002 -0.0000 -0.0000 0.0001 0.0001 0.0000 0.0001 0.0005 0.0003 0.0001 -0.000 0.000 0.000 0.000 0.000 -0.000 -0.000 0.000 -0.034 -0.002",
                "#list": [
                    -0.0003,
                    -0.0004,
                    -0.0004,
                    -0.0003,
                    -0.0001,
                    -0.0002,
                    -0.0003,
                    -0.0001,
                    0,
                    0.0002,
                    -0.0002,
                    -0.0002,
                    0,
                    0,
                    0.0001,
                    0.0001,
                    0,
                    0.0001,
                    0.0005,
                    0.0003,
                    0.0001,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    -0.034,
                    -0.002
                ]
            },
            "si:unitXMLList": {
                "#text": "\\\radian",
                "#list": [
                    "\\\radian"
                ]
            },
            "si:expandedUncXMLList": {
                "si:uncertaintyXMLList": {
                    "#text": "0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.0035 0.009 0.009 0.009 0.009 0.009 0.009 0.009 0.009 0.009 0.009",
                    "#list": [
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.0035,
                        0.009,
                        0.009,
                        0.009,
                        0.009,
                        0.009,
                        0.009,
                        0.009,
                        0.009,
                        0.009,
                        0.009
                    ]
                },
                "si:coverageFactorXMLList": {
                    "#text": "2.0",
                    "#list": [
                        2
                    ]
                },
                "si:coverageProbabilityXMLList": {
                    "#text": "0.95",
                    "#list": [
                        0.95
                    ]
                },
                "si:distributionXMLList": {
                    "#text": "normal",
                    "#list": [
                        "normal"
                    ]
                }
            }
        }
    ]
}```

# Using the libary
Hint! The Repo [dccXMLJSONConvGUI](https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConvGUI) contains a dockerfile to deploy the dcc_XMLJSONConv running on port 8000 as well as an GUI. Using this is an easy deployment option.
## Installing system dependencies:
Ubuntu:
```bash
sudo apt-get install libxml2-dev libxslt-dev python-lxml

Fedora:

sudo dnf install libxml2-devel libxslt-devel python-lxml

Installation

Methode 1. Using Git-Repo Clone Repo and install Python dependencies:

git clone https://gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv.git
cd dcc_XMLJSONConv
pip install -r ./requirements.txt

Methode 2. Use Python Pacakge 2. Alternativly install the converter as python package with pip:

pip install -e git+https://pipPull:jLBHrAnhPp9s1-qHvB5A@gitlab1.ptb.de/digitaldynamicmeasurement/dcc_XMLJSONConv.git@release#egg=dcc-XMLJSONConv

Usage of local conversion Libary:

#Imports
import json
from dccXMLJSONConv.dccConv import XMLToJson,JSONToXML


jsonDict=json.loads('COPY CONTENT FROM EXAMPLE ABOVE')
xmlSTr='COPY CONTENT FROM EXAMPLE ABOVE'
xmltoJsonDict=XMLToJson(xmlSTr)
jsontoXMLStr=JSONToXML(jsonDict)

Using the dcc_rest_server

  1. Install Additional requrenments , namly fastapi~=0.70.0 and uvicorn~=0.27.1 from requirementsServer.txt
pip install -r requirementsServer.txt
  1. Start the dcc_server with
cd ~/repos/dcc_XMLJSONConv/src/dccXMLJSONConv
(venv) seeger01@n23017:~/repos/dccXMLJSONConv/src/dcc_XMLJSONConv$ uvicorn dccServer:app --reload
INFO:     Will watch for changes in these directories: ['/home/seeger01/repos/dccXMLJSONConv/src/dccXMLJSONConv']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [16614] using StatReload
INFO:     Started server process [16616]
INFO:     Waiting for application startup.
INFO:     Application startup complete.

The REST-Server is now runing on Port 8000 use the --port and --host params to change port and host.

(venv) seeger01@n23017:~/repos/dccXMLJSONConv/src/dccXMLJSONConv$ uvicorn dccServer:app --reload --port 8001 --host 127.0.0.2
INFO:     Will watch for changes in these directories: ['/home/seeger01/repos/dccXMLJSONConv/src/dccXMLJSONConv']
INFO:     Uvicorn running on http://127.0.0.2:8001 (Press CTRL+C to quit)

REST API Documentation

This document outlines the REST API endpoints available in the application, detailing the request methods, endpoints, expected request bodies, and response formats.

Endpoints

Endpoint Method Description Request Body Response
/ GET Returns a greeting message and usage instructions. N/A { "message": "<instructions>" }
/dcc2json/ POST Converts XML string input to JSON format. { "xml": "<XML string>" } JSON object representing the original XML.
/json2dcc/ POST Converts JSON input to XML string format. { "js": <JSON object> } XML string representing the original JSON.

Details

/

  • Method: GET
  • Description: Provides a greeting message along with instructions for using the available endpoints.
  • Request Body: None
  • Response:
    • Status Code: 200 OK
    • Body:
      {
        "message": "Hello!\nBetter use the URL /json2dcc/?js={...} \n or /dcc2json (POST method)"
      }
      

/dcc2json/

  • Method: POST
  • Description: Accepts an XML string and converts it into a JSON representation.
  • Request Body: Required. The body should contain an XML string within a JSON object under the key "xml".
  • Response:
    • Status Code: 200 OK for successful conversion; 400 Bad Request for empty input.
    • Body: JSON object representing the parsed XML.

/json2dcc/

  • Method: POST
  • Description: Accepts a JSON object and converts it into an XML string.
  • Request Body: Required. The body should contain a JSON object under the key "js".
  • Response:
    • Status Code: 200 OK for successful conversion; 400 Bad Request for empty input.
    • Body: XML string representing the JSON object.

Further usage examples

For detailed usage examples, including request and response examples, refer to the automated tests provided intest_dccXMLJSONConv.py and test_dcc_server.py .

Update repeatable Fieldnames from schema URL

The skript [listTypeFinder.py][src/dcc_XMLJSONConv/listTypeFinder.py] can be used to update the list of repeated fields. Either with the functions in the skript (see Docstings for further Information) or calling the script with the XSD Url and optinal the output path like

(venv) user@host:~/repos/dccXMLJSONConv/src/dccXMLJSONConv$ python3 ./listTypeFinder.py https://ptb.de/dcc/v3.2.1/dcc.xsd tmp.json
List types have been written to tmp.json

dcc_rest_server debugging and launching with Pycharm

For Pycharm debuigging see : [https://www.jetbrains.com/help/pycharm/fastapi-project.html#run-debug-configuration}(https://www.jetbrains.com/help/pycharm/fastapi-project.html#run-debug-configuration) Create new fastApi launch configuration instead of python

Metadata

Release files for dccXMLJSONConv 2.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dccXMLJSONConv 2.3.4
File Size Uploaded Provenance Actions
dccxmljsonconv-2.3.4.tar.gz 35.3 kB Without provenance Details for dccxmljsonconv-2.3.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dccXMLJSONConv 2.3.4
File Interpreter ABI Platform Actions
dccXMLJSONConv-2.3.4-py3-none-any.whl Python 3 none any Details for dccXMLJSONConv-2.3.4-py3-none-any.whl

Total release size: 66.5 kB

Release files / dccxmljsonconv-2.3.4.tar.gz

Download URL dccxmljsonconv-2.3.4.tar.gz
Size 35.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d8808aa9c68857cb47e44dc3acd4f33b12e72be9f0614b0f9859704dafc074eb
BLAKE2b-256 checksum
How to use checksums
aaed4faf9d97ca85bb48838d03c042df1555cb107d63bfe50d8211cd7ed21ade
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Release files / dccXMLJSONConv-2.3.4-py3-none-any.whl

Download URL dccXMLJSONConv-2.3.4-py3-none-any.whl
Size 31.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90b22c7f0ed7a8ac65ca9af52a74a40a0bc408678e5ac65fc28a105a58547f7b
BLAKE2b-256 checksum
How to use checksums
82ae40d2b6f43870d9523b1148783c31845756ec3e79ede6bc46cd8c5047a6b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.7

Security for dccXMLJSONConv 2.3.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page