Forum Discussion

acesario's avatar
acesario
Contributor II
5 years ago

Is there a better way to simply convert an array to a string?

When I do a get from a given web service, I often have a single value being returned, but within an array. What happens is I end up with this type of output [“Andrew”] rather than say “Andrew”

In order to write this value, always the 0 level value I end up hacking it with something like the path for the first values.

jsonPath($, “$Student**[0]**.Student_Data[0].Student_Person_Data[0].Personal_Profile_Data[0].Date_of_Birth”)

Here is a screenshot of the input schema for reference:

8 Replies

  • acesario's avatar
    acesario
    Contributor II

    Just a comment on the impact to productivity… I have between 40 and 70 fields to map, multiplied by an average of 4 changes to the mapper to include the 0. So any real time savings of no code, gets eaten up with this tedious manual task. I also worry about using [0] in this fashion, as I was taught that this is bad practice in xslt/xpath.

    • cjhoward18's avatar
      cjhoward18
      Employee

      Hi @acesario

      I think the getFirst() method could help your scenario.

      I think the expression using getFirst() with your example would look something like this:

      $.getFirst(‘Student’).getFirst(‘Student_Data’).getFirst(‘Student_Person_Data’).getFirst(‘Personal_Profile_Data’).Date_of_Birth

      This relies on those fields always being present and arrays. If there are times that is not true additional logic will be needed.

      • acesario's avatar
        acesario
        Contributor II

        Thank you for your response @cjhoward18.
        However, my snap seems to be requiring jsonpath… if I do a getFirst (‘Student’) as prescribed I get a ‘could not compile expression’ error as shown:

        This seems to happen sometimes when pulling from a web service, where the response is structured as array [*] object. I had tried a get in places across my pipeline, and hit similar issues. Im unclear on the usage and why this occurs, but get/getfirst seem limited to certain objects.

  • acesario's avatar
    acesario
    Contributor II

    That worked, thank you for the getfirst syntax and idea. I dont mean to complain, just that this requires multiple steps per mapper row and more time and potential for human error. I have to 1. Map the field. 2. Copy the field from snaplogic to text editor 3. review the path, and 4. make adjustments to include the get first. Copy/paste back into the field in snap. (I copy out of snap because the editor window is to tight to work with.)
    This is actually more time than using [0]. Is there any advantage to using mapfirst rather than jsonpath [0]?

    • acesario's avatar
      acesario
      Contributor II

      I think I answered my own question on the downside of using [0] in a mapper, if the value is null, the expression will fail.

      • cjhoward18's avatar
        cjhoward18
        Employee

        Yeah with null safe access enabled especially it is a safer expression was my thought. I can’t see any ways to save any time from establishing/developing these mappings necessarily.

  • acesario's avatar
    acesario
    Contributor II

    @cjhoward18 interestingly, when i get data where the node does not exist (never for date of birth or name, but some other data elements) the .getfirst fails, even when using a hasOwnProperty. I ended up going back to my original pathing, with the following syntax.

    $.hasOwnProperty(“jsonPath($, ‘$Student[0].Student_Data[0].Student_Person_Data[0].Personal_Profile_Data[0].Gender_Reference.ID.Gender_Code’)”) ? jsonPath($, ‘$Student[0].Student_Data[0].Student_Person_Data[0].Personal_Profile_Data[0].Gender_Reference.ID.Gender_Code’): ‘-’