epipko
05/08/2023, 7:04 PM"amount": 0 OR "full_refund": true
I can't seem to make it work.
<cfif complete_refund is "TRUE">
<cfset shipping_refund = "full_refund": &true>
<cfelse>
<cfset shipping_refund = "amount": &0>
</cfif>
What am I doing wrong?Adam Cameron
epipko
05/08/2023, 8:29 PMMyka Forrest
05/08/2023, 8:30 PM: as an operator. Should it be part of the string?epipko
05/08/2023, 8:32 PMAdam Cameron
you seem to be attempting to use a colonHa, didn't spot that. Indeed. The picture of the code above is different though, and has the colon in the string correctly. BTW: pls never paste pictures of code. Post the actual code!as an operator:
Myka Forrest
05/08/2023, 8:39 PMepipko
05/08/2023, 8:39 PMMyka Forrest
05/08/2023, 8:39 PMepipko
05/08/2023, 8:42 PM<cfif complete_refund is "TRUE">
<cfset shipping_refund = "full_refund:" &true>
<cfelse>
<cfset shipping_refund = "amount:" &0>
</cfif>
<cfoutput>Shipping Refund: #shipping_refund#</cfoutput><cfabort>Myka Forrest
05/08/2023, 8:43 PMepipko
05/08/2023, 8:44 PMAdam Cameron
multiple
lines
all
nicely
formattedAdam Cameron
epipko
05/08/2023, 8:45 PM<cfif complete_refund is "TRUE">
<cfset shipping_refund = "full_refund:" &true>
<cfelse>
<cfset shipping_refund = "amount:" &0>
</cfif>
<cfoutput>Shipping Refund: #shipping_refund#</cfoutput><cfabort>ian.hickey
05/08/2023, 8:48 PMjson = { amount: 0 }, and call writeOutput(serializeJson(json)); it will be formatted for you.epipko
05/08/2023, 9:06 PM<cfif complete_refund is "TRUE">
<cfset shipping_refund_str = '"full_refund": true'>
<cfelse>
<cfset shipping_refund_str = '"amount": 0'>
</cfif>
<cfset shipping_refund = deserializeJSON('{' & shipping_refund_str & '}')>Myka Forrest
05/08/2023, 9:07 PMepipko
05/08/2023, 9:12 PMepipko
05/08/2023, 9:13 PM<cfquery name="q0" datasource="#REQUEST.dsn#">
select LISTAGG('{"line_item_id": '||order_item_code||' ,"quantity": '||sales_qty||', "restock_type": "return"}',',') line_items
from ep
where orig_order_id = '#orig_order_id#'
</cfquery>
<cfset line_items_str = #q0.line_items#>
<cfset line_items_array = deserializeJSON( '[' & line_items_str & ']' )>
<cfif complete_refund is "TRUE">
<cfset shipping_refund_str = '"full_refund": true'>
<cfelse>
<cfset shipping_refund_str = '"amount": 0'>
</cfif>
<cfset shipping_refund = deserializeJSON('{' & shipping_refund_str & '}')>
<cfset json_str =
{
"refund": {
"shipping": shipping_refund
,"refund_line_items":
line_items_array
}
}
>
<cfset _PAYLOAD = serializeJSON(json_str)>
<cfoutput>#_PAYLOAD#</cfoutput><cfabort>epipko
05/08/2023, 9:13 PMepipko
05/08/2023, 9:14 PMAdam Cameron
Adam Cameron
<cfif complete_refund is "TRUE">
<cfset shipping_refund = {"full_refund" = true}>
<cfelse>
<cfset shipping_refund = {"amount" = 0}>
</cfif>Adam Cameron
orig_order_id into your SQL string: pass it as a parameter.Adam Cameron
#q0.line_items#epipko
05/08/2023, 9:47 PMepipko
05/08/2023, 10:09 PM<cfquery name="q0" datasource="#REQUEST.jesta_PROD#">
select LISTAGG('{"line_item_id": '||order_item_code||' ,"quantity": '||sales_qty||', "restock_type": "return"}',',') line_items
from ep
where orig_order_id = '#orig_order_id#'
</cfquery>
<cfset line_items_str = q0.line_items>
<cfset line_items_array = deserializeJSON( '[' & line_items_str & ']' )>
ran this:
<cfquery name="q0" datasource="#REQUEST.jesta_PROD#">
select LISTAGG('{"line_item_id": '||order_item_code||' ,"quantity": '||sales_qty||', "restock_type": "return"}',',') line_items
from ep
where orig_order_id = '#orig_order_id#'
</cfquery>
<cfset line_items_array = '[' & q0.line_items & ']'>
<cfif complete_refund is "TRUE">
<cfset shipping_refund = {"full_refund" = true}>
<cfelse>
<cfset shipping_refund = {"amount" = 0}>
</cfif>
<cfset _PAYLOAD = serializeJSON(json_str)>
<cfoutput>#_PAYLOAD#</cfoutput><cfabort>
It looks like it worked and the only difference is that now json is with "\" in it. It looks like it's still valid, just looks different. Do I need to care?
{
"refund": {
"refund_line_items": "[{\"line_item_id\": 12273697456307 ,\"quantity\": 1, \"restock_type\": \"return\"},{\"line_item_id\": 12273697489075 ,\"quantity\": 1, \"restock_type\": \"return\"}]",
"shipping": {
"full_refund": true
}
}
}Adam Cameron
<cfset line_items_array = deserializeJSON( '[' & line_items_str & ']' )>
Stop it. Don't do it.
I don't know how else to say it. DO NOT BUILD JSON BY HAND.
refund_line_items is supposed to be an array, right? You've given it a string.
As well as everything else you need to look at what the code is doing, and understand it? Clearly(?) "refund_line_items": "[{\"line_item_id\": 12273697456307 ,\"quantity\": 1, \"restock_type\": \"return\"},{\"line_item_id\": 12273697489075 ,\"quantity\": 1, \"restock_type\": \"return\"}] is wrong.
Well: unless you actually do want that to be a string in the JSON object, but I really doubt it.Adam Cameron
select LISTAGG('{"line_item_id": '||order_item_code||' ,"quantity": '||sales_qty||', "restock_type": "return"}',',') line_items
Looks like yer building a partial JSON string there too. Just return the values. Stick the values into some sort of data structure. Convert the data structure to JSON last (and only once) before you - I guess - return it to the client.epipko
05/08/2023, 10:38 PMAdam Cameron
Adam Cameron
epipko
05/08/2023, 10:41 PMAdam Cameron
LISTAGG function in there, you'd just get a nice recordset along these lines wouldn't you?
line_item_id, quantity, restock_type
12273697456307, 1, "return"
12273697489075, 1, "return"epipko
05/08/2023, 10:43 PMAdam Cameron
Adam Cameron
epipko
05/08/2023, 10:43 PMAdam Cameron
Adam Cameron
Adam Cameron
Adam Cameron
<cfset line_items_array = []>
<cfloop query="q0">
<cfset line_items_array.append({
"line_item_id" = line_item_id,
"quantity" = quantity,
"restock_type" = restock_type
})>
</cfloop>
Leave the DB to do storage logic. That's what it's for. Leave CFML to do the request/response logic. That's what it's for.Myka Forrest
05/08/2023, 10:49 PMepipko
05/08/2023, 11:00 PMMyka Forrest
05/08/2023, 11:03 PMAdam Cameron
Adam Cameron
Adam Cameron
LISTAGG. I'd still use an internal DB-specific data structure, and then at the exit point convert it to JSON.Adam Cameron
evagoras
05/09/2023, 11:03 AMcfqueryparam for sql arguments
⢠build your returned array or structure first
⢠then use the serializeJSON() to turn that to a string
You never mentioned where the flag complete_refund is coming from.
It's not good REST practice to optionally include elements in your JSON. In your example, you cannot sometimes return a full_refund or amount keys and sometimes not. You always want to include them, just have it say "full_refund": true or "full_refund": false depending on the case.
I realize you might be starting out on CF, so tags it is. However, I would strongly advice you to use CFSCRIPT everywhere in your logic. Use tags only with CFM files and mixing it with HTML.
With that said, I rewrote your code using tags (yikes!) in a gist. In my example I am mocking the query but you would use the commented out one on top. Then move your complete_refund as needed.
https://trycf.com/gist/69a4701d8689f916e9abfb3d47e9896e/acf2021?theme=xcodeepipko
05/09/2023, 3:36 PMMyka Forrest
05/09/2023, 3:39 PMepipko
05/09/2023, 3:39 PMMyka Forrest
05/09/2023, 3:40 PMepipko
05/09/2023, 3:42 PMepipko
05/09/2023, 3:42 PMSELECT json_object('name' VALUE first_name || ' ' || last_name,
'hasCommission' VALUE
CASE WHEN commission_pct IS NULL THEN 'false' ELSE 'true'
END FORMAT JSON)
FROM employees WHERE first_name LIKE 'W%';
JSON_OBJECT('NAME'ISFIRST_NAME||''||LAST_NAME,'
-----------------------------------------------
{"name":"William Gietz","hasCommission":false}
{"name":"William Smith","hasCommission":true}
{"name":"Winston Taylor","hasCommission":false}Myka Forrest
05/09/2023, 3:42 PMepipko
05/09/2023, 3:42 PMevagoras
05/09/2023, 3:46 PMcfquery for the values.evagoras
05/09/2023, 4:01 PMvar in front of all your variables when declaring them inside a function to isolate them in scope within the function only.
https://trycf.com/gist/fd754f3eea7a8d8f2cc9bfbde527c051/acf2021?theme=xcodeepipko
05/09/2023, 4:08 PM