1

I am trying to create a JSON structure to send API to a Mexican invoice provider (cfdi 3.3). I already have a function with the values ​​of the items, with cfloop. The question is that I don't know how to insert in the same structure the other values ​​that are not within the array "Items:", such as payment_type, customer, etc

This is the example of API request


curl https://www.facturapi.io/v1/invoices \
  -u "sk_test_API_KEY:" \
  -H "Content-Type: application / json" \
  -d '{
    "customer": "58e93ionede86eb318b0197456",
    "items": [{
      "quantity": 2,
      "product": "58e93ionede86eb318b0197454"
    }],
    "payment_form": "06",
    "folio_number": 914,
    "series": "A"
  } '

I want to get a structure like this:

{
  "customer": "77e93ionede86eb318b0197456",
  "items": [
    {
      "quantity": 2,
      "product": "99e93etimee86eb318b0191111"
    },
    {
      "quantity": 5,
      "product": "88e55etimee86eb999b0192222"
    }
  ],
  "payment_form": "06",
  "folio_number": 914,
  "series": "A"
}

This is my code (cffunction)


<cfcomponent>
    <cfset #dsnvtasmart# = "popmart">

    <cffunction name="result" access="remote" 
            returntype="array"
            returnformat="JSON" 
            hint="it is the text">

         <cfset #dsnvtasmart# = "popmart">
         <cfquery name="qProd" datasource="#dsnvtasmart#">
            SELECT det_ordenp.cant AS quantity
                    , det_ordenp.api_id_prod AS product
            FROM    det_ordenp
            WHERE   det_ordenp.id_orden = #worden#
         </cfquery>

        <!--- 
            **---Other values Json Struct.--------**
        --->    
        <cfset customer =#77e93ionede86eb318b0197456#>
        <cfset payment_form = '06'>
        <cfset folio_number = 914>
        <cfset series = 'A'>

        <cfloop query="qProd">
            <cfset var empStruct = {"quantity":#qProd.quantity#, "product":#qProd.product#}>
            <cfset arrayAppend (result, empStruct)>
        </cfloop>

        <cfreturn result>
   </cffunction>
</cfcomponent>

At the moment, with this code, I am only getting:

[
  {
    "quantity": 6,
    "product": "5d5307eb8a6ce057e78b32be"
  },
  {
    "quantity": 37,
    "product": "5d53080d8a6ce057e78b32c0"
  }
]

I have no idea how to include in empStruct, the values ​​of :

{
  "customer": "77e93ionede86eb318b0197456",
  "payment_form": "06",
  "folio_number": 914,
  "series": "A"
}

I hope my question is readable, if anyone has a suggestion I would appreciate it.

1
  • What's your CF version? Commented Aug 14, 2019 at 21:19

1 Answer 1

2

Right now, you are only returning an array with the items. Change your function's returnType to struct and modify your code like this:

<cffunction ... returnType="struct" returnFormat="JSON">

        ...
    </cfquery>

    <cfset var response             = {}>
    <cfset response["customer"]     = "77e93ionede86eb318b0197456">
    <cfset response["items"]        = []>
    <cfset response["payment_form"] = "06">
    <cfset response["folio_number"] = 914>
    <cfset response["series"]       = "A">

    <cfloop query="qProd">
        <cfset var item = {}>
        <cfset item["quantity"] = qProd.quantity>
        <cfset item["product"]  = qProd.product>
        <cfset arrayAppend(response["items"], item)>
    </cfloop>

    <cfreturn response>
</cffunction>
  • I used ... to skip some code.
  • Using the bracket notation for structs (like response["customer"]) will guarantee the case of the key, while using the dot notation (like response.customer) may result in being serialized as "CUSTOMER": later.
  • arrayAppend(array, element) is the ugly version of array.add(element) (all CF versions), i.e. array.append(element) (CF2016+)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your support, i see your code and now I know what I have to do,I really appreciate your help and thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.