Genesys Cloud - Developer Community!

 View Only

Sign Up

  • 1.  Getting error in creating a JSON using dynamic values in Architect

    Posted 5 days ago

    I want to create a array of JSON objects with dynamic values. For that I am using a loop and inside each iteration I am trying to create the JSON object with property name and adding the created JSON object in a JSON Collection variable. 

    My expression - 

    If(!IsNotSetOrEmpty(Slot.Value),
    JsonParse("{\n  \"id\": ToInt(Task.TextID),\n  
                                 \"value\": \"Slot.Value\" \n}"),
    JsonParse("{\n  \"id\": ToInt(Task.TextID),\n 
    \"value\": \"\" \n}"))
    This is Throwing an error- Error.Expression.Value.NotAllowed. How can I resolve? 
    Also, Can I stores the array of JSON objects in JSON Collection variable??If not please suggest the way too. Actually I need to send an array of JSON objects as an input in an API. Is it possible?

    #Architect
    #DataActions

    ------------------------------
    Subhajit Podder
    NA
    ------------------------------


  • 2.  RE: Getting error in creating a JSON using dynamic values in Architect

    Posted 5 days ago

    Hey Subhajit, how are you?

    Just looking at it it's kinda hard to figure out what you are trying to achieve.

    But I'm guessing you are trying to add a JSON Object that would look like this:

    {
    id: 9999999
    value: "xxxxx"
    }

    Is that correct?

    And on the variable "TextID" you have the numerical ID(like the 99999 on my example), but as a string, right?
    As for the Slot.Value variable, you have a text (like the "xxxxx" I mentioned above). Is all of the above correct?

    Try this:

    If(!IsNotSetOrEmpty(Slot.Value),
    JsonParse("{\n  \"id\": " + Task.TextID + ",\n  \"value\": \"" + Slot.Value + "\" \n}"),
    JsonParse("{\n  \"id\": " + Task.TextID + ",\n  \"value\": \"\" \n}"))

    What seems to be wrong on your current function is that you are trying to access the value of those variables inside the string that you are creating, so the value of the variables are not being accessed correctly (I believe). And also I see no reason for you to use the "ToInt" function on the ID, as you are creating a string that will then be parsed as a JSON (As long as the ID doesn't have any double quotes outside of it, it should be treated as a number, I believe).

    Well, try the above, let me know if it helps



    ------------------------------
    Marcello Jabur
    ------------------------------



  • 3.  RE: Getting error in creating a JSON using dynamic values in Architect

    Posted 2 days ago

    Hi Mercello,

    Your understood by objective with appropriate precision. Your suggestion really worked. Thank you for being my savior. 

    Please let me know why ToInt() function is not needed. I have a numeric value as string. How did that parse as number??

    Also, I want to know the way of writing the JSON structure inside the JSONParse(). Please provide me the relevant docs or the source if possible. I didn't understand the position of single quote and the meaning.



    ------------------------------
    Subhajit Podder
    NA
    ------------------------------



  • 4.  RE: Getting error in creating a JSON using dynamic values in Architect
    Best Answer

    Posted 2 days ago

    Hi Subhajit, really glad that it worked!

    I don't have any documentations that I can provide really, but I can try to explain it.

    So, the "ToInt()" is not necessary in that case because you are actually building a string (that you then parse to a Json format using the "JsonParse()" function)

    The JsonParse() function receives a string as an input, so you don't need to use toInt() for anything. Your goal for the JsonParse() function is to have a string input with the whole json format on it. And it should have the exact format of a Json normal structure.

    So, let's recapitulate, your goal is to have this string, which would be your json object:

    "{
    id: 9999999
    value: "xxxxx"
    }"

    So the number in there (9999999) is simply part of the string in this case. When this is used like that on the JsonParse() function it will be evetually treated as an integer valur inside the Json (since you don't have double quotes outside of the number)

    That is basically this part of the function that I provided:

    JsonParse("{\n  \"id\": " + Task.TextID + ",\n  \"value\": \"" + Slot.Value + "\" \n}")

    Both the "Task.TextID" and the "Slot.Value" variables are already strings, so you don't need to use any functions to transform them into anything else (like toInt()).

    We use the "+" in there to concatenate the strings together with the variables.

    So we have the initial string ("{\n  \"id\": ")... Then we add the value of the Task.TextID variable... Then we add the second string (",\n  \"value\": \"")... Then we add the value of the Slot.Value variable... And lastly we add the third string ("\" \n}").

    That gets all added together to form a single string, that is then parsed on the JsonParse() function.

    ------------

    Hope this clarifies it a little, I'm not the best at explaining things.



    ------------------------------
    Marcello Jabur
    ------------------------------



  • 5.  RE: Getting error in creating a JSON using dynamic values in Architect

    Posted 9 hours ago

    Hi Marcello,

    Thank you for your detailed explanation. I can understand clearly and have no doubt anymore. Many many thanks @Marcello Jabur 😊



    ------------------------------
    Subhajit Podder
    NA
    ------------------------------



  • 6.  RE: Getting error in creating a JSON using dynamic values in Architect

    Posted 2 hours ago

    Really glad that I was able to help Subhajit!

    Let me know any time if I can help you with anything like that again =)



    ------------------------------
    Marcello Jabur
    ------------------------------