Expected Behavior for a!fromJson()

Hi,
We're using JSON strings to populate different SAIL components. Part of our use case involves fetching the length of an array that is stored within the JSON string. Using length() doesn't work in this case, it always returns 1. I've attached an example code snippet demonstrating this. Is this the expected behavior? If so, is there an alternative to length() that will get the correct array length?
I'm attaching screenshots of sample code showing this behavior. The first sample shows an array of length 3, when cast to and back from JSON will return length() = 1. The second sample shows that indexing the same array works as expected, and the final sample is the only workaround I could come up with. I was hoping there would be a cleaner solution than using apply over a casting function.

OriginalPostID-255970


JSON Array Behavior.docx

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer
    Just noticed the same behavior - sample showing the issue is
    length(index(a!fromJson(a!toJson({blah:{}})),"blah",{})) =1, when this should be 0 because index(a!fromJson(a!toJson({blah:{}})),"blah",{} returns {}. Seems like a bug
  • I'm not sure if this is a bug, or just more "nuance" managing types.

    There's no explicit type casting on the array.  There could easily be an "a" in the array in place of the 3.

    Appian is being flexible returning the "any type" type here even though the array is all numbers:

     

    typename(
      typeof(
        a!fromJson(
          a!toJson(
            {
              "jsonArray": {
                1,
                4,
                5,
                6
              }
            }
          )
        ).jsonArray
      )
    )
     

    and here when the array contains mixed types.

    typename(
      typeof(
        a!fromJson(
          a!toJson(
            {
              "jsonArray": {
                "oh well",
                4,
                5,
                6
              }
            }
          )
        ).jsonArray
      )
    )

    The explicit type casting looks like a good practice for this use case.

Reply
  • I'm not sure if this is a bug, or just more "nuance" managing types.

    There's no explicit type casting on the array.  There could easily be an "a" in the array in place of the 3.

    Appian is being flexible returning the "any type" type here even though the array is all numbers:

     

    typename(
      typeof(
        a!fromJson(
          a!toJson(
            {
              "jsonArray": {
                1,
                4,
                5,
                6
              }
            }
          )
        ).jsonArray
      )
    )
     

    and here when the array contains mixed types.

    typename(
      typeof(
        a!fromJson(
          a!toJson(
            {
              "jsonArray": {
                "oh well",
                4,
                5,
                6
              }
            }
          )
        ).jsonArray
      )
    )

    The explicit type casting looks like a good practice for this use case.

Children
No Data