逐行浏览json,包括未知的嵌套数组和对象

2024-09-28 22:24:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个JSON字典,可以由未知对象和数组组成,但我知道需要修改哪些键

我的代码将遍历JSON文件,如果它有某些键,比如“Date”,它将执行其他任务

该代码适用于外部键,但如果存在名为“Date”的嵌套键,则将跳过它

我需要密码逐行检查所有的钥匙

代码如下

def Converttimestamp(x):
    format = '%Y-%m-%dT%H:%M:%S.%f'

    print(x)

    for e in x:
        print(e)
        for column in ['JournalDate','CreatedDateUTC','UpdatedDateUTC','Date','DueDate','DeliveryDate','ExpectedArrivalDate','DateUTC']:
                if column in e:

                    ts = e[column]
                    print ("\n" + str(ts) + "\n")

                    ts_utc = re.split('\(|\)', ts)[1]
                    ts_utc = ts_utc[:ts_utc.find("+")]

                    e[column] = datetime.fromtimestamp(float(ts_utc)/1000).strftime(format)
                    print(str(e[column]))
    return(e)

JSON口述:

  x = {
        "InvoiceID": "8930",
        "InvoiceNumber": "Inv. 1",
        "Reference": "",
        "Payments": [
            {
                "PaymentID": "538",
                "Date": "/Date(1406246400000+0000)/",
                "Amount": 118.0,
                "Reference": "",
                "CurrencyRate": 1.0,
                "HasAccount": false,
                "HasValidationErrors": false
            }
        ],
        "AmountDue": 0.0,
        "AmountPaid": 118.0,
        "AmountCredited": 0.0,
        "CurrencyRate": 1.0,
        "Date": "/Date(1406246400000+0000)/",
        "DueDate": "/Date(1406246400000+0000)/"
        "UpdatedDateUTC": "/Date(1406246400000+0000)/"
    }

结果JSON字典:

      {
        "InvoiceID": "8930",
        "InvoiceNumber": "Inv. 1",
        "Reference": "",
        "Payments": [
            {
                "PaymentID": "538",
                "Date": "/Date(1406246400000+0000)/",
                "Amount": 118.0,
                "Reference": "",
                "CurrencyRate": 1.0,
                "HasAccount": false,
                "HasValidationErrors": false
            }
        ],
        "AmountDue": 0.0,
        "AmountPaid": 118.0,
        "AmountCredited": 0.0,
        "CurrencyRate": 1.0,
        "Date": "2014-06-30T02:00:00.000000",
        "DueDate": "2014-06-30T02:00:00.000000"
        "UpdatedDateUTC": "2014-12-15T14:08:51.843000"
    }

Tags: 代码injsonfalseformatdate字典column
1条回答
网友
1楼 · 发布于 2024-09-28 22:24:27
  • Dates位于Payments键的list内,因此必须通过访问列表内的字典来提取它

资料

data = [{'AmountCredited': 0.0,
         'AmountDue': 0.0,
         'AmountPaid': 118.0,
         'CurrencyRate': 1.0,
         'Date': '/Date(1206246400000+0000)/',
         'DueDate': '/Date(1306246400000+0000)/',
         'InvoiceID': '8930',
         'InvoiceNumber': 'Inv. 1',
         'Payments': [{'Amount': 118.0,
                       'CurrencyRate': 1.0,
                       'Date': '/Date(1406246400000+0000)/',
                       'HasAccount': False,
                       'HasValidationErrors': False,
                       'PaymentID': '538',
                       'Reference': '',
                       'TestDate': 'failtest in'},
                      {'Amount': 118.0,
                       'CurrencyRate': 1.0,
                       'Date': '/Date(1506246400000+0000)/',
                       'HasAccount': False,
                       'HasValidationErrors': False,
                       'PaymentID': '538',
                       'Reference': '',
                       'TestDate': 'failtest in in'}],
         'Reference': '',
         'TestDate': 'failtest out',
         'UpdatedDateUTC': '/Date(1606246400000+0000)/'},
        {'AmountCredited': 0.0,
         'AmountDue': 0.0,
         'AmountPaid': 118.0,
         'CurrencyRate': 1.0,
         'Date': '/Date(1206246400000+0000)/',
         'DueDate': '/Date(1306246400000+0000)/',
         'InvoiceID': '8930',
         'InvoiceNumber': 'Inv. 1',
         'Payments': [{'Amount': 118.0,
                       'CurrencyRate': 1.0,
                       'Date': '/Date(1406246400000+0000)/',
                       'HasAccount': False,
                       'HasValidationErrors': False,
                       'PaymentID': '538',
                       'Reference': '',
                       'TestDate': 'failtest in'},
                      {'Amount': 118.0,
                       'CurrencyRate': 1.0,
                       'Date': '/Date(1506246400000+0000)/',
                       'HasAccount': False,
                       'HasValidationErrors': False,
                       'PaymentID': '538',
                       'Reference': '',
                       'TestDate': 'failtest in in'}],
         'Reference': '',
         'TestDate': 'failtest out',
         'UpdatedDateUTC': '/Date(1606246400000+0000)/'}]

代码

from datetime import datetime

def to_iso(ts):
    format = '%Y-%m-%dT%H:%M:%S.%f'
    try:
        ts_utc = re.findall('\d+', ts)[0]
        return datetime.fromtimestamp(float(ts_utc)/1000).strftime(format)
    except (IndexError, TypeError):
        print(f'A key with "Date", but failed to convert the value: {ts}')
        return ts


def convert_timestamp(my_list_of_dicts: list):

    for e in my_list_of_dicts:
        # check top level keys whose values are not a list
        keys_with_date = [k for k, v in e.items() if 'Date' in k and type(v) != list]

        for k1 in keys_with_date:
            e[k1] = to_iso(e[k1])


        # check top level keys whose values are a list
        keys_with_lists = [k for k, v in e.items() if type(v) == list]

        for k1 in keys_with_lists:
            for i, d in enumerate(e[k1]):
                for k2, v in d.items():
                    if 'Date' in k2:
                        e[k1][i][k2] = to_iso(d[k2])

    return my_list_of_dicts

使用和输出

test = convert_timestamp(data)

A key with "Date", but failed to convert the value: failtest out
A key with "Date", but failed to convert the value: failtest in
A key with "Date", but failed to convert the value: failtest in in
A key with "Date", but failed to convert the value: failtest out
A key with "Date", but failed to convert the value: failtest in
A key with "Date", but failed to convert the value: failtest in in

print(test)

[{'AmountCredited': 0.0,
  'AmountDue': 0.0,
  'AmountPaid': 118.0,
  'CurrencyRate': 1.0,
  'Date': '2008-03-22T21:26:40.000000',
  'DueDate': '2011-05-24T07:13:20.000000',
  'InvoiceID': '8930',
  'InvoiceNumber': 'Inv. 1',
  'Payments': [{'Amount': 118.0,
                'CurrencyRate': 1.0,
                'Date': '2014-07-24T17:00:00.000000',
                'HasAccount': False,
                'HasValidationErrors': False,
                'PaymentID': '538',
                'Reference': '',
                'TestDate': 'failtest in'},
               {'Amount': 118.0,
                'CurrencyRate': 1.0,
                'Date': '2017-09-24T02:46:40.000000',
                'HasAccount': False,
                'HasValidationErrors': False,
                'PaymentID': '538',
                'Reference': '',
                'TestDate': 'failtest in in'}],
  'Reference': '',
  'TestDate': 'failtest out',
  'UpdatedDateUTC': '2020-11-24T11:33:20.000000'},
 {'AmountCredited': 0.0,
  'AmountDue': 0.0,
  'AmountPaid': 118.0,
  'CurrencyRate': 1.0,
  'Date': '2008-03-22T21:26:40.000000',
  'DueDate': '2011-05-24T07:13:20.000000',
  'InvoiceID': '8930',
  'InvoiceNumber': 'Inv. 1',
  'Payments': [{'Amount': 118.0,
                'CurrencyRate': 1.0,
                'Date': '2014-07-24T17:00:00.000000',
                'HasAccount': False,
                'HasValidationErrors': False,
                'PaymentID': '538',
                'Reference': '',
                'TestDate': 'failtest in'},
               {'Amount': 118.0,
                'CurrencyRate': 1.0,
                'Date': '2017-09-24T02:46:40.000000',
                'HasAccount': False,
                'HasValidationErrors': False,
                'PaymentID': '538',
                'Reference': '',
                'TestDate': 'failtest in in'}],
  'Reference': '',
  'TestDate': 'failtest out',
  'UpdatedDateUTC': '2020-11-24T11:33:20.000000'}]

相关问题 更多 >