Skip to main content
Question

How to compare two json without looking the order of their attribute?


arthy
Contributor
Forum|alt.badge.img+8
  • Contributor

Hi,

 

 

I have two json and I would like to test if they are equal without knowing their attribute name? How can I automatize it using either a transformer or python caller?

_result= [{"attributes":{"OBJECTID":objectid"ATTRIBUTE1": A1 ,  "ATTRIBUTE2": A2 ,  "ATTRIBUTE3": A3,"ATTRIBUTE4": A4, ... ,"ATTRIBUTEN": AN}}]

_result if the generic syntax of my json document.

 

 

Thanks

@takashi

3 replies

takashi
Influencer
  • July 1, 2017

Hi @arthy, assuming the feature has two JSON documents as its attributes called "_result1" and "_result2", a PythonCaller with this script determines if the two JSON documents are equal on both property names and values, and then saves the result ("yes" or "no") into a new attribute named "_same", for example.

# PythonCaller Script Example [Updated]
import json
def processFeature(feature):
    array1 = json.loads(feature.getAttribute('_result1'))
    array2 = json.loads(feature.getAttribute('_result2'))
    feature.setAttribute('_same''yes' if (array1 == array2) else 'no')

takashi
Influencer
  • July 1, 2017
takashi wrote:

Hi @arthy, assuming the feature has two JSON documents as its attributes called "_result1" and "_result2", a PythonCaller with this script determines if the two JSON documents are equal on both property names and values, and then saves the result ("yes" or "no") into a new attribute named "_same", for example.

# PythonCaller Script Example [Updated]
import json
def processFeature(feature):
    array1 = json.loads(feature.getAttribute('_result1'))
    array2 = json.loads(feature.getAttribute('_result2'))
    feature.setAttribute('_same''yes' if (array1 == array2) else 'no')
updated the script example.

 

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • July 4, 2017
takashi wrote:

Hi @arthy, assuming the feature has two JSON documents as its attributes called "_result1" and "_result2", a PythonCaller with this script determines if the two JSON documents are equal on both property names and values, and then saves the result ("yes" or "no") into a new attribute named "_same", for example.

# PythonCaller Script Example [Updated]
import json
def processFeature(feature):
    array1 = json.loads(feature.getAttribute('_result1'))
    array2 = json.loads(feature.getAttribute('_result2'))
    feature.setAttribute('_same''yes' if (array1 == array2) else 'no')
@takashi,

 

Thank you very much.

 

I really appreciated.

 

It worked.

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings