I want to retrieve a JSON DocField from a self-made DocType.
But I get it as a str(ing) instead of a normal JSON dictionary [demonstration: see below].
(1) Why is that?
Why does a JSON DocField not return a JSON dict object but a flattened serialization (as a string) of it? What use is a JSON DocField if it behaves, kind of, just like text?
Also, the string representation fills up only ONE but VERY longish line in the DT’s form, this is hardly usable for editing, especially for folding subobjects (like: Item list in Sales Order) out of the way in order to explore a biggish JSON dataset.
(2) Is there some way to retrieve the JSON DocField as an object of type dict?
The problem being that in Server Script where I need to use it, the json.loads() method isn’t available due to the Server Script restrictions.
Maybe I’m simply missing the easy way due to it not being documented, although it might exist?
If so, please hint at it here for everybody.
If not, please implement it for everybody (as a useful feature request).
Type casting issues are somewhat hard to debug in Server Script because “type()” also isn’t available.
From a bench console, which doesn’t have these restrictions, the JSON type miscasting issue can be demonstrated like this:
~/frappe-bench$ bench console
Apps in this namespace:
frappe, erpnext, raven
In [1]: SO = frappe.get_doc("SW5 JSON Object", 'm5o9nte0oj')
In [2]: type(SO)
Out[2]: frappe.model.document.Document
In [3]: SO.meta.fields
Out[3]:
[<Section BreakDocField: metadata_section parent=SW5 JSON Object>,
<DatetimeDocField: download_timestamp parent=SW5 JSON Object>,
<DataDocField: api_endpoint parent=SW5 JSON Object>,
<DataDocField: api_endpoint_filter parent=SW5 JSON Object>,
<Small TextDocField: download_url parent=SW5 JSON Object>,
<DataDocField: download_statuscode parent=SW5 JSON Object>,
<Column BreakDocField: column_break_fydh parent=SW5 JSON Object>,
<LinkDocField: linked_to_dt parent=SW5 JSON Object>,
<Dynamic LinkDocField: document_name parent=SW5 JSON Object>,
<TextDocField: notes parent=SW5 JSON Object>,
<Section BreakDocField: sw5_erpnext_mapper_section parent=SW5 JSON Object>,
<TableDocField: sw5_erpnext_mappings parent=SW5 JSON Object>,
<Section BreakDocField: json_object_section parent=SW5 JSON Object>,
<JSONDocField: sw5_json_object parent=SW5 JSON Object>]
In [4]: type(SO.sw5_json_object)
Out[4]: str
In [5]: import json
In [6]: SOj = json.loads(SO.sw5_json_object)
In [7]: type(SOj)
Out[7]: dict
In [8]: SOj.keys()
Out[8]: dict_keys(['id', 'changed', 'number', 'customerId', 'paymentId', 'dispatchId', 'partnerId', 'shopId', 'invoiceAmount', 'invoiceAmountNet', 'invoiceShipping', 'invoiceShippingNet', 'invoiceShippingTaxRate', 'orderTime', 'transactionId', 'comment', 'customerComment', 'internalComment', 'net', 'taxFree', 'temporaryId', 'referer', 'clearedDate', 'trackingCode', 'languageIso', 'currency', 'currencyFactor', 'remoteAddress', 'deviceType', 'isProportionalCalculation', 'attribute', 'customer', 'paymentStatusId', 'orderStatusId', 'dispatchMethod', 'billing', 'shipping', 'payment', 'shop', 'shipmentWeight', 'details', 'documents', 'undispatchedTrackingCodes', 'batchPickingBoxId', 'batchPickingTransactionId', 'processingWarehouseId', 'processingWarehouse', 'shippingProduct', 'pickwareAutomaticReturnLabelPrintingEnabled', 'shippingDocuments', 'printAdditionalInvoice', 'printDispatchNoteInsteadOfInvoice', 'printAdditionalDeliveryNote'])