Add first work package for REST API implementation
[anna.git] / example / diameter / launcher / resources / rest_api / helpers / diameterJsonHelper / xml2json.py
diff --git a/example/diameter/launcher/resources/rest_api/helpers/diameterJsonHelper/xml2json.py b/example/diameter/launcher/resources/rest_api/helpers/diameterJsonHelper/xml2json.py
new file mode 100755 (executable)
index 0000000..00c6326
--- /dev/null
@@ -0,0 +1,25 @@
+#!/usr/bin/python3
+import xmltodict
+import json
+import sys
+
+try:
+  file = sys.argv[1]
+except:
+  print("Usage: " + sys.argv[0] + " <diameter xml file>")
+  sys.exit(1)
+
+try:
+  with open(file, 'r') as myfile:
+    xml = myfile.read()
+except:
+  print("ERROR reading '" + file + "'")
+  sys.exit(1)
+
+# although default attribute prefix is '@', anyway we will
+# force the value just in case xmltodict implementation
+# changes. The anna::core::functions::json2xml helper,
+# assumes this prefix in order to work properly.
+my_dict=xmltodict.parse(xml, attr_prefix='@')
+json_data=json.dumps(my_dict, indent=3, sort_keys=True)
+print(json_data)