olgabotezatu
posted this
06 March 2024
- Last edited 18 March 2024
To extract the properties "numeroControl" and "codigoGeneracion" from the JSON files after customer data enrichment, you can use a scripting language like Python. Below is a Python script that demonstrates how to read the JSON data from a file and extract the desired properties:
import json
# Function to extract properties from JSON
def extract_properties(json_data):
try:
# Load JSON data
data = json.loads(json_data)
# Extract properties
numero_control = data["identificacion"]["numeroControl"]
codigo_generacion = data["identificacion"]["codigoGeneracion"]
return numero_control, codigo_generacion
except KeyError:
# Handle missing keys
print("One or more required keys not found in JSON data.")
return None, None
except json.JSONDecodeError:
# Handle invalid JSON format
print("Invalid JSON format.")
return None, None
# Example usage
json_data = '''
{
"identificacion": {
"version": 1,
"ambiente": "01",
"tipoDte": "01",
"numeroControl": "DTE-01-0000375-000000000000032",
"codigoGeneracion": "3GGG9B-3aB5-4d83-B591-311BCF7FFFB4",
"tipoModelo": 1,
"tipoOperacion": 1,
"tipoContingencia": null,
"motivoContin": null,
"fecEmi": "2025-10-11",
"horEmi": "18:56:12",
"tipoMoneda": "USD"
}
}
'''
numero_control, codigo_generacion = extract_properties(json_data)
if numero_control is not None and codigo_generacion is not None:
print("Numero Control:", numero_control)
print("Codigo Generacion:", codigo_generacion)
You can save this script into a Python file (e.g., extract_properties.py
) and run it on each JSON file you receive from Scanshare. This script will extract the "numeroControl" and "codigoGeneracion" properties from the JSON data and print them out. You can then modify the script to store these values in a database as per your requirements.