Skip to main content

Hi,

 

I have a json and I would like to format all the date and time attribute to a specific format using python.
_result= n{"attributes":{"OBJECTID":objectid, "ATTRIBUTE1": A1 ,  "DATE1": "%Y-%m-%dT%H:%M:%SZ",  "ATTRIBUTE3": A3,"DATE2": "%Y-%m-%dT%H:%M:%SZ", ... ,"ATTRIBUTEN": AN}}]

I would like to have 

_result=  {"attributes":{"OBJECTID":objectid, "ATTRIBUTE1": A1 ,  "DATE1": "%Y-%m-%d %H:%M:%S",  "ATTRIBUTE3": A3,"DATE2": "%Y-%m-%d %H:%M:%S", ... ,"ATTRIBUTEN": AN}}]

I tried this:

import datetime
import json
def formatting_date(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        for k, v in mb'attributes'].items():
            if instance (v,datetime.dateime):
                v=datetime.strptime(v,'%y-%m-%d %h:%m:%s')
    feature.setAttribute('prop_', json.dumps(array))

but it failed.

 

Any suggestions?

Can't you use the DateFormatter transformer?

That allows to set the format for multiple date attributes in one go.


What type of attribute is PROPOSALATTRIBUTES? Is it a list? If you don't know, inspect the feature in the Data Inspector.

Otherwise I agree with @erik_jan about using the DateFormatter, much easier.

 


What type of attribute is PROPOSALATTRIBUTES? Is it a list? If you don't know, inspect the feature in the Data Inspector.

Otherwise I agree with @erik_jan about using the DateFormatter, much easier.

 

@david_r,

 

I edited my question.

 

Instead of proposalattributes it should be _result which is a json array.

 

As I said to @erik_jan, I can use the dateformatted because I don't know how many attributes (or even their name) I will ahve in the json.

 

 


Can't you use the DateFormatter transformer?

That allows to set the format for multiple date attributes in one go.

@erik_jan,

 

I don't know how many date attributes or their names I will have in that json.

 

That's why I would like to have something that will do the formatting dynamically

Hi @arthy, this script converts %Y-%m-%dT%H:%M:%SZ (e.g. "2017-01-02T03:04:05Z") to %Y-%m-%d %H:%M:%S (e.g. "2017-01-02 03:04:05").

import json, datetime
def formatting_date(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        for k, v in my'attributes'].items():
            try:
                dt = datetime.datetime.strptime(v, '%Y-%m-%dT%H:%M:%SZ')
                me'attributes'].k] = dt.strftime('%Y-%m-%d %H:%M:%S')
            except:
                pass
    feature.setAttribute('prop_', json.dumps(array))

Hi @arthy, this script converts %Y-%m-%dT%H:%M:%SZ (e.g. "2017-01-02T03:04:05Z") to %Y-%m-%d %H:%M:%S (e.g. "2017-01-02 03:04:05").

import json, datetime
def formatting_date(feature):
    array = json.loads(feature.getAttribute('_result'))
    for m in array:
        for k, v in my'attributes'].items():
            try:
                dt = datetime.datetime.strptime(v, '%Y-%m-%dT%H:%M:%SZ')
                me'attributes'].k] = dt.strftime('%Y-%m-%d %H:%M:%S')
            except:
                pass
    feature.setAttribute('prop_', json.dumps(array))
@takashi

 

Thanks very much.

 

This is exactly what I was looking for.

Reply