Forum Discussion

Pretty's avatar
Pretty
New Contributor III
3 years ago

How to add data type

We have Json file as input source which some random data for that values we need to add datatypes.

Sample data:

{
“Id”: “1076”,
“jobTitle”: “Software engineer”,
“firstName”: “David”,
“lastName”: “villey”,
“fullName”: “Davidvilley”,
“email_N”: “Davidvilley@gmail.com”,
“telephoneNumber”: “+1-8474987109”
}

Please help me to add the datatypes for above data to validate.

9 Replies

  • darshthakkar's avatar
    darshthakkar
    Valued Contributor

    Hi @Pretty,

    You can use “type converter” snap to add/modify data type, let me know how it goes.

    • Pretty's avatar
      Pretty
      New Contributor III

      hello @darshthakkar @ljupcho_machkovski

      Thanks for your quick response.

      Type converter and type inspector snaps are not available in our snaplogic so is there any alternative method or snaps to achieve this?

  • @Pretty - I think we need a bit more description of what you’re trying to do here. If you just want to convert $Id to a number, you can use the built-in expression language function parseInt($Id) in a Mapper snap.

    As @ljupcho_machkovski pointed out, everything else is a String. JSON only supports a small number of data types: String, Number, Object, Array, or Boolean.

    • darshthakkar's avatar
      darshthakkar
      Valued Contributor

      Agreed, you can use a mapper for conversion to integer, string (fieldname.toString())

    • Pretty's avatar
      Pretty
      New Contributor III

      @koryknick @darshthakkar @ljupcho_machkovski

      Here exactly I am working on ,

      We have data in json file.

      {
      “Id”: “1076”,
      “jobTitle”: “Software engineer”,
      “firstName”: “David”,
      “lastName”: “villey”,
      “fullName”: “Davidvilley”,
      “email_N”: “Davidvilley@gmail.com”,
      “telephoneNumber”: “+1-8474987109”
      }

      those are the data which I mentioned above,

      firstly we will import this data from json file , But here we don’t have any datatype for this data so I need to add datatypes, from adding the datatype I can validate the data.

      For example If we add the datatype for firstName as string(4) in this case If we pass 5 character ex:David It should through an error. I need to store this error msg in separate output so to get this result i am trying to add the datatypes.

      I hope this make sense.

      Thanks in advance.

  • There is a Data Validator snap in the Flow snap pack, which is Public so you have access to it. As I stated, JSON supports a limited set of data types and does not restrict length or precision, so the Data Validator is probably your best bet.

  • alchemiz's avatar
    alchemiz
    Contributor III

    Hi @Pretty

    Good day… 😀

    See: https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1438183/Global+Functions+and+Properties#GlobalFunctionsandProperties-typeof

    {
        "Id": 1076,
        "this_is_an_array": [1, 2, 3, 4, 88],
        "this_is_an_object": {
            "key": "val"
        },
        "jobTitle": "Software engineer",
        "firstName": "David",
        "lastName": "villey",
        "fullName": "Davidvilley",
        "email_N": "Davidvilley@gmail.com",
        "telephoneNumber": "+1-8474987109",
        "this_is_a_boolean": true
    }.mapValues((val, key) => {'type': val}.mapKeys((v, k) => typeof v))
    
    

    This outputs the type as keyname

    Hope this helps

    Thanks,
    EmEm