【问题标题】:How to restructure json?如何重构json?
【发布时间】:2021-06-28 05:22:30
【问题描述】:

所以我有这个 json,我想重组它,到目前为止它在一个数组中。现在我想根据供应商对其进行排序。 现在的结构是 键1 键2 关键3

键1 键2 关键3

{
  "id": "1335735520073232385",
  "created_at": "2020-12-07 05:28:59 IST",
  "username": "kristinaforest",
  "tweet": "@iamsandyhall It’s the look of surprise on her face and the vulnerability on his ????"
  "vendor": "null"
}
{
  "id": "1335735456223399937",
  "created_at": "2020-12-07 05:28:44 IST",
  "username": "openbugbounty",
  "tweet": "@lyrsense please notify your IT security team about a security vulnerability OBB-1583432 on your website  "
  "vendor": "openbugbounty"
}
{
  "id": "1335735446496817152",
  "created_at": "2020-12-07 05:28:41 IST",
  "username": "openbugbounty",
  "tweet": "@arstechnica please notify your IT security team about a security vulnerability OBB-1583440 on your website  "
  "vendor": "openbugbounty"
}
{
  "id": "1335735260957564929",
  "created_at": "2020-12-07 05:27:57 IST",
  "username": "tpwk_falls",
  "tweet": "2 years into a song of vulnerability - a catchy melody embedded with honest lyrics opening up about a difficult time. Thank u for sharing with us  @zaynmalik I’m so proud of how far you’ve come and continue to go. Forever supporting ur journey #2yearsofgoodyears   PROUD OF ZAYN "
  "vendor": "zayn malik"
}

如何将其重组为

[
  {
    "vendor": "openbugbounty",
    "tweets": [
        {
       "id": "1335735260957564929",
       "created_at": "2020-12-07 05:27:57 IST",
       "username": "tpwk_falls",
       "tweet": "2 years into a song of vulnerability - a catchy melody embedded with            
        honest lyrics opening up about a difficult time. Thank u for sharing with us    
        @zaynmalik I’m so proud of how far you’ve come and continue to go. Forever  
        supporting ur journey #2yearsofgoodyears   PROUD OF ZAYN"
        },
       {
       "id": "1335735260957564929",
       "created_at": "2020-12-07 05:27:57 IST",
       "username": "tpwk_falls",
       "tweet": "2 years into a song of vulnerability - a catchy melody embedded with            
        honest lyrics opening up about a difficult time. Thank u for sharing with us    
        @zaynmalik I’m so proud of how far you’ve come and continue to go. Forever  
        supporting ur journey #2yearsofgoodyears   PROUD OF ZAYN  "
        }
     ]
   },
 .....

【问题讨论】:

  • 您的输入不是有效的 JSON。请提供正确的示例输入。

标签: python json jq


【解决方案1】:

你可以试试itertools.groupby。但是它有额外的开销来首先排序丢失的,因为如果一个列表像 [1,1,2,2,1,3,3] 那么groupby 将作为 [(1,1), (2,2 ), (1), (3,3)],这就是为什么需要排序的原因,显然,你可以不用 groupby,但它使代码更简单,如果你不关心时间复杂度也可以使用。

from itertools import groupby

l = [{
  "id": "1335735520073232385",
  "created_at": "2020-12-07 05:28:59 IST",
  "username": "kristinaforest",
  "tweet": "@iamsandyhall It’s the look of surprise on her face and the vulnerability on his ?",
  "vendor": "null"
},
{
  "id": "1335735456223399937",
  "created_at": "2020-12-07 05:28:44 IST",
  "username": "openbugbounty",
  "tweet": "@lyrsense please notify your IT security team about a security vulnerability OBB-1583432 on your website  ",
  "vendor": "openbugbounty"
},
{
  "id": "1335735446496817152",
  "created_at": "2020-12-07 05:28:41 IST",
  "username": "openbugbounty",
  "tweet": "@arstechnica please notify your IT security team about a security vulnerability OBB-1583440 on your website  ",
  "vendor": "openbugbounty"
},
{
  "id": "1335735260957564929",
  "created_at": "2020-12-07 05:27:57 IST",
  "username": "tpwk_falls",
  "tweet": "2 years into a song of vulnerability - a catchy melody embedded with honest lyrics opening up about a difficult time. Thank u for sharing with us  @zaynmalik I’m so proud of how far you’ve come and continue to go. Forever supporting ur journey #2yearsofgoodyears   PROUD OF ZAYN ",
  "vendor": "zayn malik"
}]

sorted_l = sorted(l, key=lambda x: x['vendor'])
grouped = groupby(sorted_l, key=lambda x: x['vendor'])

output = []

for key, group in grouped:
    output.append({
        'vendor': key,
        'tweets': [{j:i[j] for j in i if j != 'vendor'} for i in group]
    })
    
print(output)
[{'vendor': 'null',
  'tweets': [{'id': '1335735520073232385',
    'created_at': '2020-12-07 05:28:59 IST',
    'username': 'kristinaforest',
    'tweet': '@iamsandyhall It’s the look of surprise on her face and the vulnerability on his ?'}]},
 {'vendor': 'openbugbounty',
  'tweets': [{'id': '1335735456223399937',
    'created_at': '2020-12-07 05:28:44 IST',
    'username': 'openbugbounty',
    'tweet': '@lyrsense please notify your IT security team about a security vulnerability OBB-1583432 on your website  '},
   {'id': '1335735446496817152',
    'created_at': '2020-12-07 05:28:41 IST',
    'username': 'openbugbounty',
    'tweet': '@arstechnica please notify your IT security team about a security vulnerability OBB-1583440 on your website  '}]},
 {'vendor': 'zayn malik',
  'tweets': [{'id': '1335735260957564929',
    'created_at': '2020-12-07 05:27:57 IST',
    'username': 'tpwk_falls',
    'tweet': '2 years into a song of vulnerability - a catchy melody embedded with honest lyrics opening up about a difficult time. Thank u for sharing with us  @zaynmalik I’m so proud of how far you’ve come and continue to go. Forever supporting ur journey #2yearsofgoodyears   PROUD OF ZAYN '}]}]

【讨论】:

    【解决方案2】:

    您可以通过集合 defaultdict 和通过您的 json 迭代来实现这一点

    from collections import defaultdict
    
    flat_json = [{
        "id": "1335735520073232385",
        "created_at": "2020-12-07 05:28:59 IST",
        "username": "kristinaforest",
        "tweet": "@iamsandyhall It’s the look of surprise on her face and the vulnerability on his ?",
        "vendor": "null"
    },
    {
        "id": "1335735456223399937",
        "created_at": "2020-12-07 05:28:44 IST",
        "username": "openbugbounty",
        "tweet": "@lyrsense please notify your IT security team about a security vulnerability OBB-1583432 on your website  ",
        "vendor": "openbugbounty"
    },
    {
        "id": "1335735446496817152",
        "created_at": "2020-12-07 05:28:41 IST",
        "username": "openbugbounty",
        "tweet": "@arstechnica please notify your IT security team about a security vulnerability OBB-1583440 on your website  ",
        "vendor": "openbugbounty"
    },
    {
        "id": "1335735260957564929",
        "created_at": "2020-12-07 05:27:57 IST",
        "username": "tpwk_falls",
        "tweet": "2 years into a song of vulnerability - a catchy melody embedded with honest lyrics opening up about a difficult time. Thank u for sharing with us  @zaynmalik I’m so proud of how far you’ve come and continue to go. Forever supporting ur journey #2yearsofgoodyears   PROUD OF ZAYN ",
        "vendor": "zayn malik"
    }]
    
    d = defaultdict(list)
    
    for item in flat_json:
        vendor = item.pop('vendor')
        d[vendor].append(item)
    
    result = [{'vendor': k, 'tweets': v} for k, v in d.items()]
    

    【讨论】:

      【解决方案3】:
      import json
      
      array = '{ "id": "1335735520073232385", "created_at": "2020-12-07 05:28:59 IST", "username": "kristinaforest",  "tweet": "@iamsandyhall It’s the look of surprise on her face and the vulnerability on his ?","vendor": "null"}'
      data  = json.loads(array)
      print(data['id'], data['created_at'])
      [out]: 335735520073232385 2020-12-07 05:28:59 IST
      

      【讨论】:

        猜你喜欢
        • 2019-10-30
        • 1970-01-01
        • 2022-08-17
        • 1970-01-01
        • 1970-01-01
        • 2013-11-28
        • 2018-12-15
        • 2017-11-17
        • 1970-01-01
        相关资源
        最近更新 更多