Skip to main content
Question

how to remove empty property from a json with python


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

Hi,

 

 

I have a json that I'm going to use to update features on an arcgis server.

 

The syntax of my json is:
_result= [{"attributes":{"OBJECTID":objectid"ATTRIBUTE1": A1 ,  "ATTRIBUTE2": A2 ,  "ATTRIBUTE3": A3,"ATTRIBUTE4": A4, ... ,"ATTRIBUTEN": AN}}]

How can I automatically remove the entries with null values?

 

For the example above, if A2 is NULL or '' then the json should be:

 

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

I tried the fillowing code inside a python caller without success.

 

 

def remove_null(feature):
    js = feature.getAttribute('_result')
    for key, value in js.items():
        if value == None or str(value)='':  
            del js[key]
    feature.setAttribute('_result_bis',js)

Thanks

3 replies

takashi
Influencer
  • June 29, 2017

Hi @arthy, I think you have to load the source JSON document (text string) into a Python data structure before modifying the contents, then dump the result to another JSON document after modifying. You can use the Python json module. e.g.

# PythonCaller Script Example
import json
def remove_null(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        m['attributes'] = {k:v for k, v in m['attributes'].items() if v not in (None'')}
    feature.setAttribute('_result_bis', json.dumps(array))

[Addition] Another implementation example.

# PythonCaller Script Example
import json
def remove_null(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        for k, v in m['attributes'].items():
            if v == None or v == '':
                del m['attributes'][k]
    feature.setAttribute('_result_bis', json.dumps(array))

takashi
Influencer
  • June 29, 2017
takashi wrote:

Hi @arthy, I think you have to load the source JSON document (text string) into a Python data structure before modifying the contents, then dump the result to another JSON document after modifying. You can use the Python json module. e.g.

# PythonCaller Script Example
import json
def remove_null(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        m['attributes'] = {k:v for k, v in m['attributes'].items() if v not in (None'')}
    feature.setAttribute('_result_bis', json.dumps(array))

[Addition] Another implementation example.

# PythonCaller Script Example
import json
def remove_null(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        for k, v in m['attributes'].items():
            if v == None or v == '':
                del m['attributes'][k]
    feature.setAttribute('_result_bis', json.dumps(array))
BTW, why not use the JSONFormatter?

 

0684Q00000ArMLLQA3.png

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • June 29, 2017

@takashi

 

I use the python caller and it worked perfectly withe the first example of your code above.

 


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