web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Error with JSON string...
Power Automate
Answered

Error with JSON string as passed from PAD to "Run Python Script"

(0) ShareShare
ReportReport
Posted on by 60

I'm trying to take advantage of an external script's ability to perform list filtering faster than a multi-iteration loop within PAD. I have similar objects that I am comparing, each with an id property. If the "handled" list contains a matching ID to the "events" list, I want to remove that particular entry in the object. I've gotten this working in both plain JavaScript, as well as Python, but no matter what I do, I keep getting errors. The JavaScript compiler's error messages were too vague, so I switched to Python. In Python, I seem to be getting an error where the original JSON string's values are being stripped of its surrounding double quotes. This is causing Python interpreter errors. Am I handling this wrong?

 

Example of working Python, outside of PAD, (with most extraneous values removed for brevity):

events = [{"id":"11438923","title":"Event One"},{"id":"11362898","title":"Event Two"},{"id":"10734368","title":"Event Three"}]
handled = [{'id':'11438923'}]

# Extract the ids from the second object
handled_ids = set([obj['id'] for obj in handled])

# Filter out duplicates from the first object
events = [e for e in events if e['id'] not in handled_ids]

print(events)

The resulting list from the above code simply removes the first entry (matching the ID) from the events variable, and prints it out.

 

Although slightly adjusted from the above code, to handle how the values exist within my PAD flows, once run in PAD, the double-quoted strings for the object's values are seemingly gone. I can't even get past the assignment of the variable before an error is thrown. Immediately prior to entering the "Run Python script" action, if I display a Message Box with the content of the variable, the double-quotes exist.

 

Example input (JsonEvents variable, limited to one imported entry just to show its content) immediately prior to the Python action:

{"events":[{"id":"11438923","allday":"1","multiday":"1","private":"0","start":"1701406800","end":"1703393999","setup":"0","breakdown":"0","title":"Festive Fun with Felt: December Take-and-Make Craft Kits","description":"<p>Enjoy a seasonal take-and-make felt kit for December! Celebrate the start of winter and select one of three project options: Red Cardinal, Gingerbread House, or Knotted Tree. Each kit provides the pattern, directions, and materials to create and enjoy a festive felt craft. <\/p>\r\n\r\n<p>Pick up a free craft kit at the Library&rsquo;s Information Desk any day this month! One kit per visit, please; available while supplies last.<\/p>","more_info":"<p>Free printable <a href=\"https:\/\/guides.sspl.org\/FeltFun\">directions and patterns for &ldquo;Take and Make&rdquo; kits<\/a> are always available online for current and past projects.<\/p>","url":"https:\/\/sspl.libcal.com\/event\/11438923","admin_url":"https:\/\/sspl.libcal.com\/aevent.php?c=10655&m=edit&event_id=11438923","location_id":"0","location":"","audience_id":"1604|1623|1627|1628","audience":"Children|Adults|Teens|All Ages","cat_id":"64132","category":"Art, Music, and Movies > Crafting","owner":"Kali Nagler","presenter":"Miriam Meier & Jasmine Gage","cal_id":"10655","calendar":"Library Events","color":"#B3C8EF","image":"https:\/\/lcimages.s3.amazonaws.com\/data\/feat_img\/4700\/10655\/1697730913.png","geo_id":"ChIJh9RRqdA53okR1XeeOfmgCng","geo_lat":null,"geo_long":null,"cost":"0","registration":"0","registration_open":"0","seats":"","seats_taken":null,"physical_seats":null,"physical_seats_taken":null,"online_seats":null,"online_seats_taken":null,"zoom_email":null,"online_user_id":null,"online_meeting_id":null,"online_host_url":null,"online_join_url":null,"online_join_password":null,"online_provider":null,"wait_list":"0","future_dates":null}]}

 

The Python interpreter error I get when I run the action:

 File "", line 3
 handled = { 'id': 11438923, 'allday': 1, 'multiday': 1, 'private': 0, 'start': 1701406800, 'end': 1703393999, 'setup': 0, 'breakdown': 0, 'title': Festive Fun with Felt: December Take-and-Make Craft Kits, 'description': <p>Enjoy a seasonal take-and-make felt kit for December! Celebrate the start of winter and select one of three project options: Red Cardinal, Gingerbread House, or Knotted Tree. Each kit provides the pattern, directions, and materials to create and enjoy a festive felt craft. </p>
 ^
SyntaxError: unexpected token 'Fun'

 

Ignoring the second variable for now, here are the two actions in the flow to cause the issue:

BrendonKoz_0-1701902807008.png

...and the content of the Python script:

events = %JsonEvents%
events = events['events']
handled = %HandledEventsTable%

# Extract the ids from the second object
handled_ids = set([h['id'] for h in handled])

# Filter out duplicates from the first object
events = [e for e in events if e['id'] not in handled_ids]

print(events)

 

Are there any thoughts as to what I might be missing here?

  • lbendlin Profile Picture
    8,726 Super User 2026 Season 1 on at

    Strings need to be enclosed in quotes.  Your 'title'  value and others are not.  Depending on the implementation you can use single or double quotes, but you must choose one or the other.

  • BrendonKoz Profile Picture
    60 on at

    That's the thing though - they are enclosed in double quotes prior to being passed into the Python action (as are the property names, such as 'id', which retain their quotes for some reason). The 2nd code block above is an example of the text from the variable as output from a message box, immediately prior to running the Python script action.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    What's in the "%HandledEventsTable%" variable? It seems like that's a data table and not just a JSON blob. That will obviously not work, as a data table in PAD will not be interpreted as a data table in Python when you pass it in. You need to pass in a single string that can then be processed by Python. So, possibly best to pass it in as a JSON string as well. 

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • BrendonKoz Profile Picture
    60 on at

    The value of %HandledEventsTable% at the time of instantiation (I reused the variable and converted it):

    [{
     "id": "11438923",
     "allday": "1",
     "multiday": "1",
     "private": "0",
     "start": "1701406800",
     "end": "1703393999",
     "setup": "0",
     "breakdown": "0",
     "title": "Festive Fun with Felt: December Take-and-Make Craft Kits",
     "description": "<p>Enjoy a seasonal take-and-make felt kit for December! Celebrate the start of winter and select one of three project options: Red Cardinal, Gingerbread House, or Knotted Tree. Each kit provides the pattern, directions, and materials to create and enjoy a festive felt craft. </p>\r\n\r\n<p>Pick up a free craft kit at the Library&rsquo;s Information Desk any day this month! One kit per visit, please; available while supplies last.</p>",
     "more_info": "<p>Free printable <a href=\"https://guides.sspl.org/FeltFun\">directions and patterns for &ldquo;Take and Make&rdquo; kits</a> are always available online for current and past projects.</p>",
     "url": "https://sspl.libcal.com/event/11438923",
     "admin_url": "https://sspl.libcal.com/aevent.php?c=10655&m=edit&event_id=11438923",
     "location_id": "0",
     "location": "",
     "audience_id": "1604|1623|1627|1628",
     "audience": "Children|Adults|Teens|All Ages",
     "cat_id": "64132",
     "category": "Art, Music, and Movies > Crafting",
     "owner": "Kali Nagler",
     "presenter": "Miriam Meier & Jasmine Gage",
     "cal_id": "10655",
     "calendar": "Library Events",
     "color": "#B3C8EF",
     "image": "https://lcimages.s3.amazonaws.com/data/feat_img/4700/10655/1697730913.png",
     "geo_id": "ChIJh9RRqdA53okR1XeeOfmgCng",
     "geo_lat": null,
     "geo_long": null,
     "cost": "0",
     "registration": "0",
     "registration_open": "0",
     "seats": "",
     "seats_taken": null,
     "physical_seats": null,
     "physical_seats_taken": null,
     "online_seats": null,
     "online_seats_taken": null,
     "zoom_email": null,
     "online_user_id": null,
     "online_meeting_id": null,
     "online_host_url": null,
     "online_join_url": null,
     "online_join_password": null,
     "online_provider": null,
     "wait_list": "0",
     "future_dates": null
    }]
  • BrendonKoz Profile Picture
    60 on at

    In trying to put together a proof-of-concept to run through the data, I did notice that Python can't read null as an object literal value, and it must instead be "null" (a string value). I was unable to determine how to properly import the json module in PAD, so handled it with a REGEX text replacement. Now the proof-of-concept is running, but returning an empty list without error when the values obtained for the strings in the test case were obtained from the live example, so I'm kind of uncertain how/where the error(s) are coming from with the string values... I might need the Python json module, but can't figure out how to import it. Running the flow with simple test-case values works as expected (without complex string values, or the imported json module), and without error, so there's definitely something in the live variables' values that are likely not escaped properly.

     

    Working PAD Flow:

     

    SET JsonEvents TO $'''{\"events\":[{\"id\":\"11438923\",\"title\":\"Event One\"},{\"id\":\"11362898\",\"title\":\"Event Two\"},{\"id\":\"10734368\",\"title\":\"Event Three\"}]}'''
    SET HandledEventsTable TO $'''[{
     \"id\": \"11438923\",
     \"allday\": \"1\",
     \"multiday\": \"1\",
     \"private\": \"0\",
     \"start\": \"1701406800\",
     \"end\": \"1703393999\",
     \"setup\": \"0\",
     \"breakdown\": \"0\",
     \"title\": \"Festive Fun with Felt: December Take-and-Make Craft Kits\",
     \"description\": \"<p>Enjoy a seasonal take-and-make felt kit for December! Celebrate the start of winter and select one of three project options: Red Cardinal, Gingerbread House, or Knotted Tree. Each kit provides the pattern, directions, and materials to create and enjoy a festive felt craft. </p>\\r\\n\\r\\n<p>Pick up a free craft kit at the Library&rsquo;s Information Desk any day this month! One kit per visit, please; available while supplies last.</p>\",
     \"more_info\": \"<p>Free printable <a href=\\\"https://guides.sspl.org/FeltFun\\\">directions and patterns for &ldquo;Take and Make&rdquo; kits</a> are always available online for current and past projects.</p>\",
     \"url\": \"https://sspl.libcal.com/event/11438923\",
     \"admin_url\": \"https://sspl.libcal.com/aevent.php?c=10655&m=edit&event_id=11438923\",
     \"location_id\": \"0\",
     \"location\": \"\",
     \"audience_id\": \"1604|1623|1627|1628\",
     \"audience\": \"Children|Adults|Teens|All Ages\",
     \"cat_id\": \"64132\",
     \"category\": \"Art, Music, and Movies > Crafting\",
     \"owner\": \"Kali Nagler\",
     \"presenter\": \"Miriam Meier & Jasmine Gage\",
     \"cal_id\": \"10655\",
     \"calendar\": \"Library Events\",
     \"color\": \"#B3C8EF\",
     \"image\": \"https://lcimages.s3.amazonaws.com/data/feat_img/4700/10655/1697730913.png\",
     \"geo_id\": \"ChIJh9RRqdA53okR1XeeOfmgCng\",
     \"geo_lat\": null,
     \"geo_long\": null,
     \"cost\": \"0\",
     \"registration\": \"0\",
     \"registration_open\": \"0\",
     \"seats\": \"\",
     \"seats_taken\": null,
     \"physical_seats\": null,
     \"physical_seats_taken\": null,
     \"online_seats\": null,
     \"online_seats_taken\": null,
     \"zoom_email\": null,
     \"online_user_id\": null,
     \"online_meeting_id\": null,
     \"online_host_url\": null,
     \"online_join_url\": null,
     \"online_join_password\": null,
     \"online_provider\": null,
     \"wait_list\": \"0\",
     \"future_dates\": null
    }]'''
    Text.Replace Text: JsonEvents TextToFind: $'''([^\"\"])(null)([^\"\"])''' IsRegEx: True IgnoreCase: True ReplaceWith: $'''$1\"null\"$3''' ActivateEscapeSequences: False Result=> JsonEvents
    Text.Replace Text: HandledEventsTable TextToFind: $'''([^\"\"])(null)([^\"\"])''' IsRegEx: True IgnoreCase: True ReplaceWith: $'''$1\"null\"$3''' ActivateEscapeSequences: False Result=> HandledEventsTable
    @@copilotGeneratedAction: 'False'
    Scripting.RunPythonScript PythonCode: $'''events = %JsonEvents%
    handled = %HandledEventsTable%
    events = events[\'events\']
    
    # Extract the ids from the second object
    handled_ids = set([h[\'id\'] for h in handled])
    
    # Filter out duplicates from the first object
    events = [e for e in events if e[\'id\'] not in handled_ids]
    
    print(events)''' ScriptOutput=> PythonScriptOutput ScriptError=> ScriptError

     

     

     

  • BrendonKoz Profile Picture
    60 on at

    Okay, now it's working with the live values if they're assigned manually in my test case. 🤦‍♂️ Compilers are fun when they work differently the next day/session. I'll have to try converting null values to a string "null" in the live version (tomorrow) and see what happens.

  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    If you want to import a module that is not installed with the PAD package (i.e. when you try to import and it doesn't find it), you need to install Python to your machine and then provide the path to the modules directory to the Run Python script action. This will then allow you to import other modules. 

     

    But be aware that this only works with Python 2.x versions. PAD does not natively support Python 3 scripts.

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Verified answer
    BrendonKoz Profile Picture
    60 on at

    In the end, I'm not 100% sure what worked to get around my initial issue; it never appeared again. @Agnius' tip about how to reference Python modules was exactly what I needed to know to properly import modules (I wrongly assumed since PAD supposedly used IronPython, that it was already preinstalled somewhere.) I was unable to get PAD's Python to properly convert the JSON string into a Python object; it might've been that my formatting of the string was wrong, or there was something else that I was missing...either way, I found a trick to handle nulls that simply declared `null` as a variable, and assigned it Python's null value (`null = None`) which solved my null value issue in converting the JSON to a Python object (they're so similar, it just became an object, essentially).

     

     

     

     

    import json
    
    null = None
    
    events = %JsonEvents%
    handled = %HandledEventsTable%
    events = events['events']
    
    # Extract the ids from the second object
    handled_ids = set([h['id'] for h in handled])
    
    # Filter out duplicates from the first object
    events = [e for e in events if e['id'] not in handled_ids]
    
    print(json.dumps(events))

     

     

     

     

    After exporting the string back to PAD via the `print` statement, I noticed there was a BOM character preceeding the intended value. In order to strip this, I used a simple find/replace REGEX, primarily because I couldn't figure out how to prevent the BOM from being returned from within the Python code itself.

     

    Text.Replace Text: PythonOutput TextToFind: $'''^.*\\[''' IsRegEx: True IgnoreCase: True ReplaceWith: $'''[''' ActivateEscapeSequences: True Result=> PythonOutput

     

    NOTE: PythonOutput is the variable name that the "Run Python script" action created in this example.

     

    After that, I converted the JSON string back to a custom object for PAD to continue doing what it needed to do with my list of custom objects.

     

    Definitely a hacky solution that I'm not 100% proud of, but sometimes we just have to move forward and don't have time to look back. I still don't know why I was having string quote issues with my first attempts, and since that was never really solved, I'm not sure if marking any response here as a solution is appropriate, but I wanted to follow up and at least provide what my solution ended up being in case it helps someone else in the future.

     

    Additional notes:

    1. PAD doesn't (currently) handle null values as provided initially from a JSON string all too well. If accessing a property that has a null value, PAD will error and say the property doesn't exist (in fact its value doesn't exist). To workaround that, change the value of `null` to an empty string.
      BrendonKoz_0-1702407662482.png
    2. Python, as how my code is written above, will double-escape backslashes, which were inserted to escape forward slashes. During conversion back, this causes the string to remain escaped (ex: http:// would instead be http:\/\/), so before converting from a JSON string back to a custom object in PAD, I had to replace "\\" with "\" (non-regex).
    3. Be aware with any/all string replacements that contains external content; you might unintentionally replace something you didn't intend to. The word part of "null" might've actually been intended within usable textual content. In my case that is unlikely, but your case might be different. If that's the scenario, your find/replace might need to be different in order to account for that.
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Thanks for sharing your solution. I'll take the liberty of marking it as the solution to the topic.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Automate

#1
Mohsin Ali Profile Picture

Mohsin Ali 426

#2
11manish Profile Picture

11manish 345 Super User 2026 Season 2

#3
M Bilal Khan Profile Picture

M Bilal Khan 308

Last 30 days Overall leaderboard