Skip to main content
Question

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

  • June 30, 2017
  • 6 replies
  • 430 views

arthy
Contributor
Forum|alt.badge.img+8

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?

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.

6 replies

erik_jan
Contributor
Forum|alt.badge.img+26
  • 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
Celebrity
  • 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

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

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
Celebrity
  • 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

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.