Skip to main content
Question

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

  • June 30, 2017
  • 3 replies
  • 108 views

arthy
Contributor
Forum|alt.badge.img+8

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

This post is closed to further activity.
It may be an old question, an answered question, an implemented idea, or a notification-only post.
Please check post dates before relying on any information in a question or answer.
For follow-up or related questions, please post a new question or idea.
If there is a genuine update to be made, please contact us and request that the post is reopened.

3 replies

takashi
Celebrity
  • 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
Celebrity
  • 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')
updated the script example.

 

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • July 4, 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,

 

Thank you very much.

 

I really appreciated.

 

It worked.