Casting dates to and from JSON

Hi,

So when Appian writes dates to Json, it cannot cast those values correctly if it reads them back.

load(
  local!data:{
    today()
  },
  local!jsonData: a!toJson(
    local!data
  ),
  local!fromJson: cast(
    typeof(today()),
    a!fromJson(
      local!jsonData
    )
  ),  
  a!sectionLayout(
    contents: {
      a!textField(
        value: local!jsonData
      ),
      a!textField(
        value: local!fromJson
      )
    }
  )
)

Do I have to manually alter the string in order to reorder the date from "YYYY-MM-DDZ" to "DD-MM-YYYYZ" before I run from Json?

Is there a reason that Appian designed it this way?

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Lead Developer

    I would suggest adhere to the XML standard for date time as while generating the JSON from Appian, Appian would do so as well. Then  while converting it back from JSON you need to convert it to xml format and then cast to datetime.

    ToJson :

    a!toJson(
      value: {
        datestamp: now()
      }
    )


    FromJSON :
    load(
      local!jsonData: a!fromJson(
        "{""datestamp"":""2018-11-13T04:54:46.029Z""}"
      ).datestamp,
      if(
        regexmatch(
          "^([0-9]+\d[-][0-9]\d[-][0-9]\d[T][0-9]\d[:][0-9]\d[:][0-9]\d[\.][0-9]+[Z]+)$",
          local!jsonData
        ),
        torecord(
          toxml(
            local!jsonData
          ),
          now()
        ),
        null
      )
    )


    you may need the regex if you can't rely on consumer sending the proper data as per xml standards  defined and agreed before hands. Might have to tweak the regex as well.
     

  • Huh thats really interesting, however I'm having a couple of problems.

    Firstly, I think that regex function is a plugin rather than Appian basic so it seems questionable that it would be Appians recommendation. Although they might reccomend the same just with the out of the box text functions.

    Secondly my Appian installation code suggestor thingy does not suggest that I use torecord() ever and if I manually enter it, I'm not given any details in that little box that usually contains a description and parameter. Given that I can see that Appian has documentation for this in 18.3 I assume this is a problem with my installation. (It does function it just acts like the function is meant to be hidden).

    And finally, if we cannot handle CDTs and have to craft a custom parser rule why not just use datetext? I assume that it would be less expensive.

  • 0
    Certified Lead Developer
    in reply to jonathanb0001

    Regex functions do come from a plugin - shared component which is cloud approved as well. 


    I checked the previous release documents and can trace back the torecord() to 17.1 the oldest supported version. But frankly speaking, don't remember when was this introduced. Seems it had always been there. Something odd with your installation.


    datetext I am assuming you want to convert the datetime to a string first - this will give you a string notation of user's local time. then while converting it back to datetime you need to honor the GMT offset. parse the string and get this offset, use local on top of it. Appian wouldn't automatically convert this from a string. Then you have to make sure consumers always pass you the string in an agreed format (that's why I thought of XML standard as Appian seems to be doing this anyway).

    And if you have this agreement in place, you don't need regex at all (as suggested in the original post :) ).
    Regex is just a check before attempting a conversion if your agreement on a format is not in place or there is a potential of this being consumed at a large scale (at the enterprise level). You don't want to rely on consumer always passing the data in the correct format. Otherwise, you can ignore the regex code and just convert it to datetime. if the data is not proper your conversion will fail.

     

Reply
  • 0
    Certified Lead Developer
    in reply to jonathanb0001

    Regex functions do come from a plugin - shared component which is cloud approved as well. 


    I checked the previous release documents and can trace back the torecord() to 17.1 the oldest supported version. But frankly speaking, don't remember when was this introduced. Seems it had always been there. Something odd with your installation.


    datetext I am assuming you want to convert the datetime to a string first - this will give you a string notation of user's local time. then while converting it back to datetime you need to honor the GMT offset. parse the string and get this offset, use local on top of it. Appian wouldn't automatically convert this from a string. Then you have to make sure consumers always pass you the string in an agreed format (that's why I thought of XML standard as Appian seems to be doing this anyway).

    And if you have this agreement in place, you don't need regex at all (as suggested in the original post :) ).
    Regex is just a check before attempting a conversion if your agreement on a format is not in place or there is a potential of this being consumed at a large scale (at the enterprise level). You don't want to rely on consumer always passing the data in the correct format. Otherwise, you can ignore the regex code and just convert it to datetime. if the data is not proper your conversion will fail.

     

Children
No Data