Process Model Cant Write a List to the DB

My process model has an integration task that calls an API and then follows up to write into my database, but I keep getting this error when I run it:

Cannot index property 'property' of type Text into empty List of Dictionary) (Data Outputs)

From what I'm understanding is that the PM can't write a List ({}) to the DB and rightfully so. So in my expression for the output I have this:

if(
    length(
        rule!myIntegration().result.body.people.person.address
    ) =0,"",
    rule!myIntegration().result.body.people.person.address.state.value)

I try to tell the expression, that if the List ({}) is empty (or length of 0), then to make it an empty string ("") so it can write an empty string to the DB instead of a List (why the error happens), otherwise if not empty or not 0, to add the value to the DB. I would think this work but I keep getting this error. My JSON API string is as follow:

{    
    "people": [
        {
            "id": 1,
            "person": {
                "address": { 
                    "state": { "value": "VA" }
                }
            }
        },
        {
            "id": 2,
            "person": {
                "address": {}
            }
        }
    ]
}


As you can see, the second user is what I'm trying to take into account in the expression. I've tried it many different ways and for some reason I still get this error.

  Discussion posts and replies are publicly visible

Parents
  • The issue that you are facing here is not related to the "process model unable to write a list to DB", although that's a different thing to discuss, here in your expression rule, you are trying to check if the length of the array of "address" is empty or not, if it is, then return an empty string, otherwise, return all the states' "value".

    Now, if we look closely at the JSON that you provided, your "if" condition will go into the false block because out of two "state", you have one as non-null. But, at the same time, when you're trying to fetch the states' "value" in your false block, you are trying to fetch the data for the second user's state as well, which is null. Thus, you're getting this error "Expression evaluation error: Invalid index: Cannot index property 'state' of type Text into empty List of Dictionary".

    To fix this, you can try replacing your expression with the following snippet:

    if(
        length(rule!myIntegration().result.body.people.person.address) = 0,
        "",
        reject(
          fn!isnull,
          a!forEach(
            items: rule!myIntegration().result.body.people.person.address,
            expression: index(fv!item, "state", "value", null)
          )
        )
    )

    Hope it helps!

  • That seemed to make the error go away, but a side effect of that is that it captures the next users info into the one where its empty and then the rest of the users have the next users values.

  • I got a little confused. Could you please elaborate on your use case?

  • Sure: So what happen is that the user in ID 2 (one with the empty List), instead of having an empty string ("") it took the next users state value.

    JSON:

    {    
        "people": [
            {
                "id": 2,
                "person": {
                    "address": {}
                }
            },
            {
                "id": 3,
                "person": {
                    "address": { 
                        "state": { "value": "TX" }
                    }
                }
            },
            {
                "id": 4,
                "person": {
                    "address": { 
                        "state": { "value": "WI" }
                    }
                }
            },
        ]
    }

    In the Database now: User ID 2 has User IDs 3 state.value ("TX") instead of empty string as in the if statement suggested, then User Id 3 has User Id 4 state.value ("WI") and so forth.

    Hopefully that makes better sense.

Reply
  • Sure: So what happen is that the user in ID 2 (one with the empty List), instead of having an empty string ("") it took the next users state value.

    JSON:

    {    
        "people": [
            {
                "id": 2,
                "person": {
                    "address": {}
                }
            },
            {
                "id": 3,
                "person": {
                    "address": { 
                        "state": { "value": "TX" }
                    }
                }
            },
            {
                "id": 4,
                "person": {
                    "address": { 
                        "state": { "value": "WI" }
                    }
                }
            },
        ]
    }

    In the Database now: User ID 2 has User IDs 3 state.value ("TX") instead of empty string as in the if statement suggested, then User Id 3 has User Id 4 state.value ("WI") and so forth.

    Hopefully that makes better sense.

Children