Reputation: 13
I have this piece of code to extract the values form JSON. It works when parsing strings, but it has issues with the numbers. For example I always receive value (Approved or 0.1) when getting the "workflowStep" or "revision" but I always receive "-" when trying to get the numeric value form JSON. Any idea how to tweak the code to get the numeric values out of JSON.
At this stage I'm not sure if it is the numeric value or nesting the value inside "test" header.
import clr
clr.AddReference('System.Web.Extensions') # Add reference to the System.Web.Extensions assembly
from System.Web.Script.Serialization import JavaScriptSerializer # Import JavaScriptSerializer
# Assuming _loopAPIstatus and _looplabeldata are available in the context
status_code = _loopAPIstatus.Value
api_response = _looplabeldata.Value
def get_label_state(api_response, status_code):
# Directly convert status_code to an integer
try:
status_code = int(status_code) # Ensure the status code is an integer
except ValueError:
return "InvalidStatusCode", "", "", "" # Handle invalid status codes
# Initialize variables for label details
final_label_state = "" # Initialize the temporary variable with a default value
label_width_in_units = ""
label_height_in_units = ""
revision = ""
# Check the HTTP status code
if status_code == 200:
try:
# Initialize the JavaScriptSerializer to parse JSON
serializer = JavaScriptSerializer()
# Parse the JSON response
response_data = serializer.DeserializeObject(api_response)
# Extract the workflowStep value
if "workflowStep" in response_data:
final_label_state = response_data["workflowStep"]
else:
final_label_state = "workflowStepNotFound"
# Extract the label_width_in_units value
if "labelWidthInUnits" in response_data:
width_value = response_data["labelWidthInUnits"]
label_width_in_units = response_data["labelWidthInUnits"]
#else:
label_width_in_units = "-"
# Extract the labelHeightInUnits value
if "labelHeightInUnits" in response_data:
label_height_in_units = response_data["labelHeightInUnits"]
else:
label_height_in_units = "-"
# Extract the revision value
if "revision" in response_data:
revision = response_data["revision"]
else:
revision = "-"
except Exception:
final_label_state = "Error"
elif status_code == 404:
final_label_state = "NotFound"
else:
final_label_state = "UnknownStatus"
# Return the results
return final_label_state, label_width_in_units, label_height_in_units, revision
# Now update the _labelstate.Value with the final result
label_state, label_width_in_units, label_height_in_units, revision = get_label_state(api_response, status_code)
# Update _labelstate.Value with the final label state
_labelstate.Value = label_state
# Update individual variables for label details
_labelwidth.Value = label_width_in_units
_labelheight.Value = label_height_in_units
_revision.Value = revision
The JSON values I'm trying to get:
{
"test": [
{
"name": "test",
"author": null,
"description": null,
"title": null,
"workstation": null,
"labelWidth": 70000.0,
"labelWidthInUnits": 70.0,
"labelHeight": 100000.0,
"labelHeightInUnits": 100.0,
"unit": "Millimeter",
"dataSets": []
}
],
"workflowStep": "Approved",
"revision": "0.1",
"checkedOut": false,
"readOnly": false,
"canCheckIn": false,
"canCheckOut": false,
"canDiscardCheckOut": false,
"checkedInRevision": null,
"nextMajorRevision": null,
"nextMinorRevision": null,
"metadata": []
}
Upvotes: 0
Views: 25