Forum Discussion

amit_saroha's avatar
amit_saroha
New Contributor III
4 years ago

Reading JSON Format

Hi,

How I can use an expression to read the below in a mapper to get individual values true or false? I am using as - $DocumentId.startsWith(“EBSPERSON_”)

[

{

"Content_ID":

"EBSPERSON_547229"

}

{

"Content_ID":

"EBSPERSONAD_547229"

}

{

"Content_ID":

"EBSPHONE_547229"

}

]

18 Replies

  • @amit.saroha
    Hope this helps

    jsonPath($, "$[?(value.Document_ID.startsWith('EBSPERSON_'))].Document_ID")

    The idea is to use the filter operator. It will select all the element/s in the Array that match the given expression. In the above example, my filter condition is based on the value of ‘Document_ID’

    • amit_saroha's avatar
      amit_saroha
      New Contributor III

      @Dheeraj - Thank you for all the help but I am still facing the issue. I think it’s not JSON in Document_ID but it is string (see screenshot # 2). Could you suggest how to overcome it?


      h

      • Dheeraj's avatar
        Dheeraj
        Employee

        @amit.saroha
        Would you be able to download and share the preview of the upstream snap connected to the mapper snap(labeled
        ‘Person File’) shared in the screenshot above

  • bojanvelevski's avatar
    bojanvelevski
    Valued Contributor

    Per my understanding, you need to get the fields out of objects in array, that their value starts with “EBSPERSON_”. If that’s so, try the following expression:

    $Document_ID.map(x=>x.filter((v,k)=> v.startsWith('EBSPERSONAD_')))

  • amit_saroha's avatar
    amit_saroha
    New Contributor III

    Below is the actual data and I want to pass the value “EBSPERSON_547229” in the target path.

    • Dheeraj's avatar
      Dheeraj
      Employee

      Use below expression :

      jsonPath($, "$_Document_ID[?(value.Content_ID.startsWith('EBSPERSON_'))].Content_ID")

  • I see your JSON is not formatted properly. I would recommend validating the JSON snippet using some external validators
    You can also attach your pipeline here and I can look into that

  • @amit.saroha
    I won’t be able to validate your pipeline as it needs accounts to connect to the systems. In scenarios like this, it would be great if you can export the data to a JSON generator snap and work on the expressions it would be a lot simpler and easier to debug.

    I am attaching a sample pipeline where I tried to replicate your data and worked on the expression.
    Test Expr - Community_2022_06_28.slp (3.6 KB)

    • amit_saroha's avatar
      amit_saroha
      New Contributor III

      @Dheeraj , thanks for your help but the sample data is not like the way I have. I have the below data coming out of mapper -

      In the another mapper when I use it the below error comes -