Python – 如何将嵌套的 JSON 字典移动到其自己的索引位置?

huangapple go评论91阅读模式
英文:

Python - How can I move a nested json dictionary up to its own index?

问题

  1. {
  2. "classification": [
  3. {
  4. "classificationId": "Cash",
  5. "taxonomyId": "accounting.gp"
  6. }
  7. ],
  8. "id": "235",
  9. "kind": "Real",
  10. "name": "Checking"
  11. },
  12. {
  13. "classification": [
  14. {
  15. "classificationId": "Cash",
  16. "taxonomyId": "accounting.gp"
  17. }
  18. ],
  19. "id": "236",
  20. "kind": "Real",
  21. "name": "Cash Reserve"
  22. }
英文:

I have a json dataset where each item/index can contain 2 nested dictionaries. The problem is that one of these nested dictionaries contains all of the exact key:value pairs as its parent dictionary. To put it in other words, I have a parent "Account" and any time there are "Sub-Accounts" it places the Sub-Accounts in the nested dictionary, and they are never seen as their own standalone item/index.

Here is the sample json of one item/index. Essentially, I need the sub_accounts object to extracted and become its own index. As you can see, it contains all of the same key:value objects as the parent containins the sub_accounts.

  1. {
  2. "classification": [
  3. {
  4. "classificationId": "Cash",
  5. "taxonomyId": "accounting.gp"
  6. }
  7. ],
  8. "id": "235",
  9. "kind": "Real",
  10. "name": "Checking",
  11. "sub_accounts": [
  12. {
  13. "classification": [
  14. {
  15. "classificationId": "Cash",
  16. "taxonomyId": "accounting.gp"
  17. }
  18. ],
  19. "id": "236",
  20. "kind": "Real",
  21. "name": "Cash Reserve",
  22. "sub_accounts": []
  23. }
  24. ]
  25. },

I have been able to use json_normalize or even variations of .pop() to accomplish a flattening of data and I have tried to explore other flattening options, but with no luck on the specific task I am trying to accomplish. Those solutions usually just result with the subaccounts still be associated to the original index.

答案1

得分: 2

你可以使用递归函数来遍历层次结构,逐步弹出"sub_accounts"键:

  1. def extractAccounts(accounts):
  2. return
    展开收缩
    )))]

从一个帐户对象列表:

  1. data = [{
  2. "classification": [
  3. {
  4. "classificationId": "Cash",
  5. "taxonomyId": "accounting.gp"
  6. }
  7. ],
  8. "id": "235",
  9. "kind": "Real",
  10. "name": "Checking",
  11. "sub_accounts": [
  12. {
  13. "classification": [
  14. {
  15. "classificationId": "Cash",
  16. "taxonomyId": "accounting.gp"
  17. }
  18. ],
  19. "id": "236",
  20. "kind": "Real",
  21. "name": "Cash Reserve",
  22. "sub_accounts": []
  23. }
  24. ]
  25. }]

输出:

  1. accounts = extractAccounts(data)
  2. for i,account in enumerate(accounts):
  3. print("Account #",i)
  4. print(account)

输出结果如下:

  1. Account # 0
  2. {'classification': [{'classificationId': 'Cash', 'taxonomyId': 'accounting.gp'}], 'id': '235', 'kind': 'Real', 'name': 'Checking'}
  3. Account # 1
  4. {'classification': [{'classificationId': 'Cash', 'taxonomyId': 'accounting.gp'}], 'id': '236', 'kind': 'Real', 'name': 'Cash Reserve'}

如果你的顶级是单个帐户(即不是列表),在调用函数时只需将其放在列表中:extractAccount([data])

英文:

You could use a recursive function to traverse the hierarchy while progressively popping out the "sub_accounts" keys:

  1. def extractAccounts(accounts):
  2. return
    展开收缩
    )))]

From a list of account objects:

  1. data = [{
  2. "classification": [
  3. {
  4. "classificationId": "Cash",
  5. "taxonomyId": "accounting.gp"
  6. }
  7. ],
  8. "id": "235",
  9. "kind": "Real",
  10. "name": "Checking",
  11. "sub_accounts": [
  12. {
  13. "classification": [
  14. {
  15. "classificationId": "Cash",
  16. "taxonomyId": "accounting.gp"
  17. }
  18. ],
  19. "id": "236",
  20. "kind": "Real",
  21. "name": "Cash Reserve",
  22. "sub_accounts": []
  23. }
  24. ]
  25. }]

Output:

  1. accounts = extractAccounts(data)
  2. for i,account in enumerate(accounts):
  3. print("Account #",i)
  4. print(account)
  5. Account # 0
  6. {'classification': [{'classificationId': 'Cash', 'taxonomyId': 'accounting.gp'}], 'id': '235', 'kind': 'Real', 'name': 'Checking'}
  7. Account # 1
  8. {'classification': [{'classificationId': 'Cash', 'taxonomyId': 'accounting.gp'}], 'id': '236', 'kind': 'Real', 'name': 'Cash Reserve'}

If your top level is a single account (i.e. not a list), just place it in a list when calling the function: extractAccount([data])

答案2

得分: 0

以下是您要翻译的代码部分:

  1. raw_data = """
  2. [
  3. {
  4. "classification": [
  5. {
  6. "classificationId": "Cash",
  7. "taxonomyId": "accounting.gp"
  8. }
  9. ],
  10. "id": "235",
  11. "kind": "Real",
  12. "name": "Checking",
  13. "sub_accounts": [
  14. {
  15. "classification": [
  16. {
  17. "classificationId": "Cash",
  18. "taxonomyId": "accounting.gp"
  19. }
  20. ],
  21. "id": "236",
  22. "kind": "Real",
  23. "name": "Cash Reserve",
  24. "sub_accounts": []
  25. }
  26. ]
  27. }
  28. ]
  29. """
  30. import json
  31. jdict = json.loads(raw_data)
  32. empty_list = list()
  33. result = list()
  34. for elem in jdict:
  35. sub_elem_list = elem['sub_accounts']
  36. elem['sub_accounts'] = empty_list
  37. result.append(elem)
  38. for sub_elem in sub_elem_list:
  39. result.append(sub_elem)
  40. print(json.dumps(result, indent=4))
  41. output = """
  42. [
  43. {
  44. "classification": [
  45. {
  46. "classificationId": "Cash",
  47. "taxonomyId": "accounting.gp"
  48. }
  49. ],
  50. "id": "235",
  51. "kind": "Real",
  52. "name": "Checking",
  53. "sub_accounts": []
  54. },
  55. {
  56. "classification": [
  57. {
  58. "classificationId": "Cash",
  59. "taxonomyId": "accounting.gp"
  60. }
  61. ],
  62. "id": "236",
  63. "kind": "Real",
  64. "name": "Cash Reserve",
  65. "sub_accounts": []
  66. }
  67. ]
  68. """
英文:

I don't have a generic answer, but this seems to do what you need:

  1. raw_data = """
  2. [
  3. {
  4. "classification": [
  5. {
  6. "classificationId": "Cash",
  7. "taxonomyId": "accounting.gp"
  8. }
  9. ],
  10. "id": "235",
  11. "kind": "Real",
  12. "name": "Checking",
  13. "sub_accounts": [
  14. {
  15. "classification": [
  16. {
  17. "classificationId": "Cash",
  18. "taxonomyId": "accounting.gp"
  19. }
  20. ],
  21. "id": "236",
  22. "kind": "Real",
  23. "name": "Cash Reserve",
  24. "sub_accounts": []
  25. }
  26. ]
  27. }
  28. ]
  29. """
  30. import json
  31. jdict = json.loads(raw_data)
  32. empty_list = list()
  33. result = list()
  34. for elem in jdict:
  35. sub_elem_list = elem['sub_accounts']
  36. elem['sub_accounts'] = empty_list
  37. result.append(elem)
  38. for sub_elem in sub_elem_list:
  39. result.append(sub_elem)
  40. print(json.dumps(result, indent=4))
  41. output = """
  42. [
  43. {
  44. "classification": [
  45. {
  46. "classificationId": "Cash",
  47. "taxonomyId": "accounting.gp"
  48. }
  49. ],
  50. "id": "235",
  51. "kind": "Real",
  52. "name": "Checking",
  53. "sub_accounts": []
  54. },
  55. {
  56. "classification": [
  57. {
  58. "classificationId": "Cash",
  59. "taxonomyId": "accounting.gp"
  60. }
  61. ],
  62. "id": "236",
  63. "kind": "Real",
  64. "name": "Cash Reserve",
  65. "sub_accounts": []
  66. }
  67. ]
  68. """

When you have nested structures you need to nest your loops. The other answer has recursion, which can cause problems if you're nesting over a thousand recursive calls (so probably not this case). I also assumed that you care about order, preferring the parent's id to be first. Also, if you're trying to get rid of the sub_accounts from the json, then you'd want to pop it from the records, but I again assume that the structure should be maintained.

huangapple
  • 本文由 发表于 2023年2月18日 03:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定