Wonder if someone can help me understand and fix t...
# cfml-beginners
e
Wonder if someone can help me understand and fix this: I am trying to create .json file. When I dump variable - all looks good, but once I serialize it, for some reasons, "\" are added.
b
@epipko That's because you have an encoded JSON string inside of your JSON
You'd need to how us how you're building this, but it would appear that the
fulfillment_order_line_items
key in your
fulfillment_json
struct is a string, not a nested struct
You can see this from the dump you pasted
Basically everything is working correctly here, but I'm not clear if you are wanting your data in another format or just don't understand how JSON escapes double quotes that appear inside a string (which is already quoted)
e
Thank you for your reply.
I pull data from database table (Oracle) this way:
select * from ep;
FULFILLMENT_LINE_ID LINE_ITEM_ID UNITS_PAKD 12254030430387 12100547903667 1 12254030463155 12100547936435 1
<cfquery name="q1" datasource="#REQUEST.T#">
select LISTAGG('{'||'"id":'||fulfillment_line_id||','||'"quantity":'||units_pakd||'}', ',') line_items
from ep
</cfquery>
Above query returns: {"id":12254030430387,"quantity":1},{"id":12254030463155,"quantity":1} <cfset fulfillment_json = { "fulfillment": { "location_id": "30406673", "line_items_by_fulfillment_order": [ { "fulfillment_order_id": #fulfillment_order_id#, "fulfillment_order_line_items": [ #q1.line_items# ] } ], "tracking_info": { "number": #invoiced_ecomm_orders.trkg_nbr#, "url": "www.usps.com" }, "notify_customer": false, "message": "Your package was shipped." } } />
<cfset _PAYLOAD = serializeJSON(fulfillment_json)>
b
@epipko Right, that's working as written.
If you were wanting that struct to be a first class citizen of the JSON, then you'll need to deserialize it first, then stick it in your fulfillment_order_line_items array, THEN re-serialize the whole thing.
It also doesn't help that your variable names are confusing.
fulfillment_json
, for example, does NOT contain JSON, but instead a CFML struct. (JSON is a string)
e
Thank you sooo very much for your help. You helped me a lot. Your example is very easy to understand. I will have to learn what serializeJSON and deserializeJSON actually do and I think it will help me in the future.