Skip to main content
Question

how to format all the date and time properties from a json to a specific format using python


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

Hi,

 

I have a json and I would like to format all the date and time attribute to a specific format using python.
_result= [{"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 m['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?

6 replies

erik_jan
Contributor
Forum|alt.badge.img+17
  • Contributor
  • June 30, 2017

Can't you use the DateFormatter transformer?

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


david_r
Evangelist
  • June 30, 2017

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.

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • June 30, 2017
david_r wrote:

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.

 

 


arthy
Contributor
Forum|alt.badge.img+8
  • Author
  • Contributor
  • June 30, 2017
erik_jan wrote:

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

takashi
Influencer
  • July 1, 2017

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 m['attributes'].items():
            try:
                dt = datetime.datetime.strptime(v, '%Y-%m-%dT%H:%M:%SZ')
                m['attributes'][k] = dt.strftime('%Y-%m-%d %H:%M:%S')
            except:
                pass
    feature.setAttribute('prop_', json.dumps(array))

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

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 m['attributes'].items():
            try:
                dt = datetime.datetime.strptime(v, '%Y-%m-%dT%H:%M:%SZ')
                m['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.

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