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
    over 4 years ago

    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.
     

Reply
  • 0
    Certified Lead Developer
    over 4 years ago

    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.
     

Children