逐行浏览json,包括未知的嵌套数组和对象 [英] Go through json line by line including unkown nested arrays and objects

查看:49
本文介绍了逐行浏览json,包括未知的嵌套数组和对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的代码将通过JSON



该代码适用于外键,但如果有嵌套键,则该文件将执行其他任务。



名称为 Date,它已被跳过。



我需要代码逐行浏览所有键。



下面的代码。

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

print(x)

对于x中的e:
print(e)
用于['JournalDate'中的列, ''CreatedDateUTC','UpdatedDateUTC','Date','DueDate','DeliveryDate','ExpectedArrivalDate','DateUTC']:
如果e中的列为:

ts = e [列]
打印( \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(格式)
print(str(e [co [column]))
return(e)

JSON字典:

  x = {
InvoiceID: 8930,
InvoiceNumber : Inv。 1,
参考:,
付款:[
{
PaymentID: 538,
Date: / Date(1406246400000 + 0000)/,
金额:118.0,
参考:,
CurrencyRate:1.0,
HasAccount:false,
HasValidationErrors:假
}
],
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,
参考:,
付款:[
{
PaymentID: 538,
Date: / Date(1406246400000 + 0000)/,
金额:118.0,
参考:,
CurrencyRate:1.0,
HasAccount:false,
HasValidationErrors:假
}
],
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
}


解决方案


  • 日期列表中在 Payments 键中,因此必须通过访问列表内的字典来提取它。



数据

  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',
'付款':[{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'/ Date(1406246400000 + 0000)/',
'HasAccount':否,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate':'在'}中失败测试,​​
{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'/ Date(1506246400000 + 0000)/',
' HasAccount':False,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate':'failtest in '}],
'参考':'',
'TestDate':'失败测试',
'UpdatedDateUTC':'/ Date(1606246400000 + 0000)/'},
{'AmountCre dited':0.0,
'AmountDue':0.0,
'AmountPaid':118.0,
'CurrencyRate':1.0,
'Date':'/ Date(1206246400000 + 0000 )/',
'DueDate':'/ Date(1306246400000 + 0000)/',
'InvoiceID':'8930',
'InvoiceNumber':'Inv。 1',
'付款':[{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'/ Date(1406246400000 + 0000)/',
'HasAccount':否,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate':'在'}中失败测试,​​
{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'/ Date(1506246400000 + 0000)/',
' HasAccount':False,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate':'failtest in '}],
'参考':'',
'TestDate':'失败测试',
'UpdatedDateUTC':'/ Date(1606246400000 + 0000)/'}]



代码



  from datetime导入datetime 

def to_iso(ts):
格式='%Y-%m-%dT%H:%M:%S.%f'
试试:
ts_utc = re.findall( '\d +',ts)[0]
返回datetime.fromtimestamp(float(ts_utc)/ 1000).strftime(format)
除外(IndexError,TypeError):
print(f '带有日期的键,但未能转换值:{ts}')
return ts


def convert_timestamp(my_list_of_dicts:list):

在my_list_of_dicts中的e:
#检查其值不在列表中的顶级键
keys_with_date = [k为k,e.items()中的v,如果k中的'Date'并键入(v)!=列表] keys_with_date中k1的


e [k1] = to_iso(e [k1])$ ​​b

$ b#检查顶部值为列表的级别键
keys_with_lists = [k对于k,e.items()中的v,如果type(v)== list]

对于keys_with_list中的k1 s:i的
,enumerate(e [k1])的d:k2的
,d.items()的v:如果k2中的日期,则

e [ k1] [i] [k2] = to_iso(d [k2])

return my_list_of_dicts



用法和输出



  test = convert_timestamp(data)

带有 Date的键,但未能转换值:failtest out
带有 Date的键,但未能转换值:
带有日期的键,failtest但未能转换值:
中的一个带 Date的键失败,但未能转换值:无法测试
具有 Date的一个键,但未能转换值:
具有日期的键,但未能转换值:

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',
'付款':[{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'2014-07-24T17:00:00.000000',
'HasAccount':否,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate': 'failtest in'},
{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'2017-09-24T02:46:40.000000',
'HasAccount':否,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate':'failtest in in'}],
'参考':'',
'TestDate':'失败测试',
'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',美元b $ b DueDate: 2011-05-24T07:13:20.000000,
InvoiceID: 8930,
InvoiceNumber: Inv。 1',
'付款':[{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'2014-07-24T17:00:00.000000',
'HasAccount':否,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate': 'failtest in'},
{'Amount':118.0,
'CurrencyRate':1.0,
'Date':'2017-09-24T02:46:40.000000',
'HasAccount':否,
'HasValidationErrors':False,
'PaymentID':'538',
'Reference':'',
'TestDate':'failtest in in'}],
'参考':'',
'TestDate':'失败测试',
'UpdatedDateUTC':'2020-11-24T11:33:20.000000'} ]


I have a JSON dictionary that can be composed of unknown objects and arrays, but I know which keys I need to modify.

My code will go through the JSON file and if it has certain keys say, 'Date' it will perform other tasks.

The code works for the outer keys, but if there is a nested key with the name 'Date', it is being skipped.

I need the code to go through all the keys, line by line.

Code below.

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 Dictonary:

  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)/"
    }

Result JSON Dictionary:

      {
        "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"
    }

解决方案

  • Dates is inside a list inside of the Payments key therefore it must be extracted by accessing the dictionary inside the list.

Data

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)/'}]

Code

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

Usage and output

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'}]

这篇关于逐行浏览json,包括未知的嵌套数组和对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆