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 / How to stop loop once ...
Power Automate
Answered

How to stop loop once reached the last element of json?

(0) ShareShare
ReportReport
Posted on by 357

Hi Team, 

I have json file which contains data such as :

Sanket1_0-1718624079225.png

"31": {
        "raw_material_table": {
            "headers": [
                "Sr. No",
                "Raw Material",
                "Molecular Wt.",
                "Moles",
                "Mole Ratio",
                "Quantity",
                "Source",
                "A. R. No.",
                "Purity"
            ],
            "rows": [
                {
                    "Sr. No""01.",
                    "Raw Material""Aprepitant",
                    "Molecular Wt.""534.0",
                    "Moles""0.0936",
                    "Mole Ratio""1.0",
                    "Quantity""50.0 gm",
                    "Source""Fos/R.18n/851/03/ FosIR184/851/144",
                    "A. R. No.""-",
                    "Purity""-"
                },
                {
                    "Sr. No""02.",
                    "Raw Material""TBPP .",
                    "Molecular Wt.""538.0",
                    "Moles""0.130",
                    "Mole Ratio""1.39",
                    "Quantity""70.0 8",
                    "Source""foste.187 (8511080,",
                    "A. R. No.""-",
                    "Purity"""
                },
                {
                    "Sr. No""03",
                    "Raw Material""THE cory)",
                    "Molecular Wt.""-",
                    "Moles""-",
                    "Mole Ratio""10vol",
                    "Quantity""500ml",
                    "Source""8.2 . fine.",
                    "A. R. No.""-",
                    "Purity""-"
                },
I want to check this 31 number is present or not in json file it should be dynamic.

I want to stop the loop once I reached to final section of that in pic 111 .
I am looping by using the while loop 
I have use the if condition : IF JsonAsCustomObject Contains var_FormattedTablecounter
When my counter passes 111 still is going in then part means True

Thanks in Advance!!!

I have the same question (0)
  • CU16071609-1 Profile Picture
    6,255 Moderator on at

    @Sanket1 

     

    It appears to be a challenging task. I assume you have JSON data that you are converting into a custom object. It would likely be easier to obtain the number of keys (1,2,3...n) from the JSON values using a scripting language. Then, you can treat this as the number of rows in your custom object, which would allow you to determine when to terminate your loop.

     

    If you believe this proposed logic is suitable for the use case, please inform me and I will provide further assistance on this matter.

     


    Thanks,
    Deenuji Loganathan 👩‍💻
    Automation Evangelist 🤖
    Follow me on LinkedIn 👥

    -------------------------------------------------------------------------------------------------------------
    If I've helped solve your query, kindly mark my response as the solution ✔ and give it a thumbs up!👍 Your feedback supports future seekers 🚀

  • Sanket1 Profile Picture
    357 on at

    Could you please provide me the script for the same?
    I always appreciate your valuable suggestion as well as solutions.

  • Sanket1 Profile Picture
    357 on at

    Hi Team 
    I have got the solution for this problem.
    I have Vb script to get keys from json files.
    I am getting keys from json by using regex pattern.

    This is my script

    ' Create a FileSystemObject
    Set fs = CreateObject("Scripting.FileSystemObject")

    ' Define the path to your JSON file
    Dim filePath
    filePath = "C:\Users\sanket.shinde\Documents\@JsonExtraction\LNB-PRD-093.json" ' Replace with your actual file path

    ' Array to store extracted numbers
    Dim numbers()
    ReDim numbers(-1)

    ' Check if the file exists
    If fs.FileExists(filePath) Then
        ' Read JSON content from the file
        Dim jsonContent
        Set file = fs.OpenTextFile(filePath, 1) ' 1: ForReading
        jsonContent = file.ReadAll()
        file.Close
       
        ' Regular expression pattern to extract all numbers followed by a colon
        Dim regexPattern
        regexPattern = """(\d+)""\s*:"
       
        ' Create a RegExp object
        Dim regex
        Set regex = New RegExp
       
        ' Set properties for the RegExp object
        regex.Global = True ' Match all occurrences
        regex.IgnoreCase = True
        regex.MultiLine = True
        regex.Pattern = regexPattern
       
        ' Execute the regular expression on the JSON string
        Dim matches
        Set matches = regex.Execute(jsonContent)
       
        ' Check if matches are found
        If matches.Count > 0 Then
            ' Resize the array to hold all matches
            ReDim numbers(matches.Count - 1)
           
            ' Iterate over each match
            For i = 0 To matches.Count - 1
                ' Extract the matched number (from the first capturing group)
                numbers(i) = CInt(matches(i).SubMatches(0)) ' Convert to integer
            Next
           
            ' Find the largest number in the array
            Dim largestNumber
            largestNumber = numbers(0) ' Initialize with the first element
           
            For i = 1 To UBound(numbers)
                If numbers(i) > largestNumber Then
                    largestNumber = numbers(i)
                End If
            Next
           
            ' Print the largest number found
            WScript.Echo "Largest number: " & largestNumber
        Else
            WScript.Echo "No numbers followed by colon found in the JSON."
        End If
    Else
        WScript.Echo "File not found: " & filePath
    End If

    Thank you so much Team!!!😊



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

Congratulations to the July Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
David_MA Profile Picture

David_MA 247 Super User 2026 Season 2

#2
11manish Profile Picture

11manish 164 Super User 2026 Season 2

#3
trice602 Profile Picture

trice602 138 Super User 2026 Season 2

Last 30 days Overall leaderboard