Escape charaters in JSON causing issue while retrieving attribute in ForEach activity

sachin gupta 376 Reputation points
2021-09-20T02:27:43.33+00:00

I have below JSON

{
            "id": " https://xxx.vault.azure.net/secrets/xxx ",
            "attributes": {
                "enabled": true,
                "nbf": 1632075242,
                "created": 1632075247,
                "updated": 1632075247,
                "recoveryLevel": "Recoverable+Purgeable"
            },
            "tags": {}
        }

The above JSON is the output of a web activity and I am using this output into a ForEach activity. The above output when goes to ForEach activity as input, all the values are coming with escape characters.

{

{**\"id\":\" https://xxx.vault.azure.net/secrets/\*\*xxx\*\*\*\* \",\"attributes\":{\"enabled\":true,\"nbf\":1632075242,\"created\":1632075247,\"updated\":1632075247,\"recoveryLevel\":\"Recoverable+Purgeable\"},\"tags\":{}}

From this JSON, I am trying to get only xxx value from the id attribute. How can I do this in Dynamic expression.
Any help is much appreciated.

Thanks

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
0 comments No comments
{count} votes

Accepted answer
  1. MartinJaffer-MSFT 26,236 Reputation points
    2021-09-20T21:32:40.137+00:00

    Hello @sachin gupta and welcome to Microsoft Q&A.

    If you just want to replace \" with " , then you can use the below:

    replace(pipeline().parameters.input,'\"','"')  
    

    However I suspect you also want to read it as JSON not text, in which case you need:

    @json(replace(pipeline().parameters.input,'\"','"'))  
    

    Do note that foreach expects an array at some point.

    sometimes @json alone is enough to remove escape characters.

    For extracting the xxx , I notice you have two sets of xxx in the id attribute. I am assuming you only want the last one, the one following /secrets/ .

    ----------

    Given sample data:

    [  
    {\"id\":\" https://abc.vault.azure.net/secrets/abc \",\"attributes\":{\"enabled\":true,\"nbf\":1632075242,\"created\":1632075247,\"updated\":1632075247,\"recoveryLevel\":\"Recoverable+Purgeable\"},\"tags\":{}},  
    {\"id\":\" https://xyz.vault.azure.net/secrets/xyz \",\"attributes\":{\"enabled\":true,\"nbf\":1632075242,\"created\":1632075247,\"updated\":1632075247,\"recoveryLevel\":\"Recoverable+Purgeable\"},\"tags\":{}}  
    ]  
    

    and ForEach items expression:

    @json(replace(pipeline().parameters.sample_data,'\"','"'))  
    

    and a Set Variable activity inside the loop with expression:
    @substring(item().id,
    add(lastindexof(item().id,'/'),1),
    sub(length(item().id), add(lastindexof(item().id,'/'),1))
    )

    I get outputs abc , xyz

    133753-image.png


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.