Skip to main content

Notifications

Community site session details

Community site session details

Session Id : z8jYgRVA0jsFTlyi9HCTIG
Power Automate - Building Flows
Answered

Comparing 2 JSONs with same keys to get the differences

Like (1) ShareShare
ReportReport
Posted on 14 Oct 2024 23:48:05 by 6
Hello, I need to compare two JSONs with the same keys, check which keys has different values and isolate those keys and their value. I tried to use the Filter Array function, but it keeps returning me the JSON I put in "From". Do you guys have an idea on how to do this? This is the opposite of the case answered here: https://community.powerplatform.com/forums/thread/details/?threadid=2e9b9f69-2963-42c9-a29d-23d70fc00bb4
 
Thank you.
 
JSON1
[
{
"Key1" : "aaa",
"Key2" : "bbb",
"Key3" : "ccc",
"Key4" : "ddd",
"Key5" : "eee",
"Key6" : "fff",
"Key7" : "ggg",
"Key8" : "hhh",
"Key9" : "iii",
"Key10" : "jjj"
}
]
 
JSON2
[
{
"Key1" : "xxx",
"Key2" : "bbb",
"Key3" : "ccc",
"Key4" : "ddd",
"Key5" : "yyy",
"Key6" : "fff",
"Key7" : "ggg",
"Key8" : "hhh",
"Key9" : "zzz",
"Key10" : "jjj"
}
]
 
The result should be:
[
{
"Key1" : "xxx",
"Key5" : "yyy",
"Key9" : "zzz"
}
]
  • CU14102321-0 Profile Picture
    6 on 15 Oct 2024 at 22:50:05
    Comparing 2 JSONs with same keys to get the differences
    Hello, guys. Thanks for the quick response.
    All solutions worked for me!!
    The Keys are different from each other within the JSON and identical when compared to the other JSON. I numbered the keys to differentiate them. And I actually know all the keys.
     
    Thank you very much.
  • Verified answer
    Chriddle Profile Picture
    7,708 Super User 2025 Season 1 on 15 Oct 2024 at 12:44:33
    Comparing 2 JSONs with same keys to get the differences
    Another approach with intersection()
     
    concat(
        if(
            and(
                equals(intersection(outputs('Compose'), outputs('Compose_2'))?[item()], null),
                not(equals(outputs('Compose_2')?[item()], null))
            ),
            concat(
                '"', item(), '":',
                '"', outputs('Compose_2')[item()], '"'
            ),
            '***foo***'
        )
    )
     
     
    json(
        concat(
            '{',
            replace(
                replace(
                    join(body('Select_3'), ','),
                    '***foo***,',
                    ''
                ),
                '***foo***',
                ''
            ),
            '}'
        )
    )
     
  • Chriddle Profile Picture
    7,708 Super User 2025 Season 1 on 15 Oct 2024 at 10:22:31
    Comparing 2 JSONs with same keys to get the differences
    For simplicity I've extracted the objects from the JSON's array in "Compose" and "Compose 2".
    If your JSONs contain arrays with multiple items, you need to surround this with an Apply to each (and, of course, adapt the expressions accordingly).
     
    I assume that you don't know the object's property names in advance and get them with the help of xpath()
    Be aware that xpath's name function encodes characters that are not alowed in XML node names. If you encounter problems there (e.g. with spaces), check the output of the "Select" and handle this with the function replace()
     
    If you know the property names in advance, just remove the "Select" (that gets these names) and put them as an array of strings into the "Filter array"'s From.
     
     
    Result:
     
     
    Select
    From:
    xpath(
    	xml(json(concat('{"Root":{"Item":', outputs('Compose'),'}}'))),
    	'//Item/*'
    )
    Map:
    xpath(
    	item(),
    	'name(/*)'
    )
     
    Filter array
    From:
    body('Select')
    Filter:
    outputs('Compose')?[item()]
    is not equal to
    outputs('Compose_2')?[item()]
     
    Select 2
    From:
    body('Filter_array')
    Map:
    concat(
    	'"', item(), '":',
    	'"', outputs('Compose_2')?[item()], '"'
    )
     
    Compose 3
    json(
    	concat(
    		'{',
    		join(body('Select_2'), ','),
    		'}'
    	)
    )
     
  • Suggested answer
    takolota1 Profile Picture
    4,861 Super User 2025 Season 1 on 15 Oct 2024 at 03:39:23
    Comparing 2 JSONs with same keys to get the differences
    Is your data really 2 different JSON objects each containing multiple columns you need to check, or is it 2 JSON arrays with a “Key” column in each object in the array?
     
    Initially lets assume your example is just bad & you actually have…
    JSON1
    [
    {
    “Key”:”aaa”
    },
    {
    “Key”:”bbb”
    },
    {
    ”Key”:”ccc”
    }
    ]
     
    JSON2
    [
    {
    “Key”:”aaa”
    },
    {
    “Key”:”eee”
    },
    {
    ”Key”:”ccc”
    }
    ]
     
    If your JSON objects have key value pairs other than just “Key” Use Select actions with the Map input tabbed to a single box input & the input expression item()[‘Key’] to get arrays of just the Key values for each JSON array.
    Then use the intersect( ) expression on your two arrays to get only an array of values that are the same between the two. From there you can then use a Filter array action where the From is a union( ) of the two sets of values & the Filter condition is where the intersect( ) output Does not contain item(). That way it filters to all values not contained in the list of all similar values, in other words the different values.

     
    If it is really like your example data with 2 large JSON objects then you’ll likely need to use the string( ) expression on your JSON objects to stringify them & then use some split( ) expression on “, to get an array of values for each object. From there you can then use the same intersect( ) & union( ) & Filter array pattern to get those that are not the same.
     

    No premium connectors & no loops required.
  • Suggested answer
    SudeepGhatakNZ Profile Picture
    14,318 Most Valuable Professional on 15 Oct 2024 at 03:16:28
    Comparing 2 JSONs with same keys to get the differences
    There are two ways to tackle this
     
    Option 1:
     
    You can use the union() function to merge both JSON arrays into one and then loop through this merged array. For each item, you can check if the corresponding value in both arrays is mismatched. While loops are generally discouraged in larger datasets due to performance concerns, this approach works well for small datasets.
     
    Option 2:
    The second option is to use the Premium actions as suggested in this response

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Congratulations to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Power Automate - Building Flows

#1
stampcoin Profile Picture

stampcoin 105

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 90 Super User 2025 Season 1

#3
David_MA Profile Picture

David_MA 62 Super User 2025 Season 1

Overall leaderboard