Python解析对象中的json对象

pkwftd7m  于 2023-01-22  发布在  Python
关注(0)|答案(1)|浏览(157)

我已经成功地用python阅读和解析了一个json字符串,包括规范化和分解数组,但是我还有第二个json格式,我正在努力,而且进展不大。
我需要拉出“数据”对象中的“实体”和“转换”。
我的json结构是:

{
  "model": {
    "id": "639b2970ac4d16767484b2bd",
    "name": "TestImport",
    "description": "",
    "type": "LineageModel",
    "referenceModelType": null,
    "owner": {
      "id": "639b2904ac4d167674849e1e",
      "name": "xyz",
      "firstName": null,
      "lastName": null
    },
    "members": {
      "totalUsers": 1,
      "totalGroups": 0,
      "users": {
        "Owner": [
          {
            "joinTime": "2022-12-15T14:04:32.076Z",
            "modificationTime": "2022-12-15T14:04:32.076Z",
            "email": "sxyz",
            "username": null,
            "hasPendingSiteInvite": false,
            "isDisabled": false,
            "id": "639b2904ac4d167674849e1e",
            "name": "xyz",
            "firstName": null,
            "lastName": null
          }
        ]
      },
      "groups": {}
    },
    "$type": "ModelInformation"
  },
  "data": {
    "version": "simple",
    "roots": [
      "8a44e4d6-4062-4e1c-a46c-c7787cab4405",
      "d1494635-9005-4337-8eab-227265b29332"
    ],
    "entities": {
      "29380f60-620e-4314-9969-4ad6fe5bbea6": {
        "name": "Element",
        "children": [],
        "id": "29380f60-620e-4314-9969-4ad6fe5bbea6",
        "properties": {}
      },
      "86361ab4-6002-4f3b-b6ca-7e35acd69f9b": {
        "name": "Application",
        "children": [
          "29380f60-620e-4314-9969-4ad6fe5bbea6"
        ],
        "id": "86361ab4-6002-4f3b-b6ca-7e35acd69f9b",
        "properties": {}
      },
      "223d9749-feb2-425d-b512-17b5322cda96": {
        "name": "S_Group",
        "children": [
          "86361ab4-6002-4f3b-b6ca-7e35acd69f9b"
        ],
        "id": "223d9749-feb2-425d-b512-17b5322cda96",
        "properties": {}
      }
    },
    "transitions": {
      "c4e2026a-9c57-4bb0-b2e2-f7068d9c6fe5": {
        "source": "29380f60-620e-4314-9969-4ad6fe5bbea6",
        "target": "040677a5-820f-4d17-ae50-1296c0e36273",
        "id": "c4e2026a-9c57-4bb0-b2e2-f7068d9c6fe5",
        "properties": {}
      }
    },
    "metadata": {
      "queries": "{\"version\":\"3\"}",
      "views": "{\"views\":[{\"name\":\"MainView\",\"description\":\"\",\"applyOnLoad\":true,\"view\":{\"version\":20,\"zoom\":{\"scale\":1,\"layerWidth\":250,\"layerSpacing\":40},\"collapsed\":{\"expanded\":[\"961847ad-1693-47b5-aa29-88ee07700b5e\",\"0da88727-e272-44a4-adeb-5a0465970490\",\"223d9749-feb2-425d-b512-17b5322cda96\",\"86361ab4-6002-4f3b-b6ca-7e35acd69f9b\"],\"collapsed\":[]},\"trace\":{\"enabled\":true,\"lock\":null,\"highlightedTraceDepth\":1,\"isHighlightedTraceDepthAll\":true,\"isTraversable\":true},\"selection\":[],\"queries\":{\"styled\":[],\"filtered\":[],\"filterType\":{\"6eecd266-2c2e-46d1-a00a-ceec18a87cdb\":\"show\"},\"expandedModules\":[]},\"settings\":{\"hideEmptyContainers\":false,\"hideFilteredLayers\":false,\"expandFilteredEntities\":false,\"portHintsEnabled\":true,\"autoBundleTransitions\":1,\"autoStyleTransitions\":1,\"autoHideTransitions\":1,\"maxSpanningTransitionDepth\":10,\"rootEntityType\":\"Layer\"}},\"options\":{\"zoom\":true,\"collapsed\":true,\"trace\":true,\"selection\":true,\"queries\":true,\"settings\":true},\"id\":\"VIEW-tcPIi3jP\"}]}"
    },
    "queries": [
      {
        "id": "9eb5c7fe-8d4c-4d94-b1a7-fee08bb2f663",
        "name": "TestID",
        "description": "",
        "displayRules": "[{\"id\":\"e119af2d-5061-43f3-901a-4c70f402bb5d\",\"type\":\"PROPERTY\",\"staticColour\":\"#E47833\",\"dynamicColouring\":false,\"cls\":\"dr-19\",\"property\":\"TestID\",\"prefix\":\"\",\"suffix\":\"\",\"center\":false,\"left\":false}]",
        "querySource": "not isEmpty(TestID)",
        "modulePath": "Uncategorised",
        "importedFromModel": null
      }
    ],
    "propertyDefinitions": {
      "propertyDefinitionsId": "2ab6a0c3-8d24-4235-9783-e241437bf860",
      "modelId": "639b2970ac4d16767484b2bd",
      "properties": {
        "TestID": {
          "type": "Number",
          "defaultValue": null,
          "options": [],
          "optionInfos": {}
        }
      }
    }
  },
  "importedModels": {},
  "importedModelsForQueries": {},
  "propertyDefinitionsForImports": {},
  "templateCollections": {}
}

我一直在使用下面的代码将JSON转换为 Dataframe :

fInput = 'filepath to json file'
with open(fInput, encoding='utf-16') as inputfile:
    df = pd.read_json(inputfile)

fOutput = 'output file path'
df.to_csv(fOutput, encoding='utf-16, index = false)

然后,我使用对其他json格式有效的方法规范化列

pd.json_normalize(df['column'])

我用以下公式分解列中的数组:

df2 = pd.DataFrame([d, tup.id) for df.intertuples() for d in tup.columnName])

我不能解决的是如何将“数据”对象中的“实体”对象拉入数据框架,一旦我能做到这一点,我就应该能够解析内容。
我不得不:

df = df["data"]
df = df["entities"]

当我打印出来时,它看起来很有希望,但是如果我尝试输出到csv,它会失败,并显示“'dict'对象没有属性”,所以我在某个地方出错了。

AttributeError
Input [48], in <cell line: 14>()
   12 df = df["entities"]
   13 print(df)
   14 df.to_csv(fOutput, encoding='utf-16', index=false)
AttributeError: 'dict' object has no attribute 'to_csv'

多谢指点。

yyyllmsg

yyyllmsg1#

你就不能这么做吗?

我将您提供的json数据保存在名为data.txt的文件中

import json

with open('data.txt','r') as file:
    data = json.load(file)

entities = data['data']['entities']
transitions = data['data']['transitions']

print(f'Entities: {entities}\nTransitions: {transitions}')

输出

Entities: {'29380f60-620e-4314-9969-4ad6fe5bbea6': {'name': 'Element', 'children': [], 'id': '29380f60-620e-4314-9969-4ad6fe5bbea6', 'properties': {}}, '86361ab4-6002-4f3b-b6ca-7e35acd69f9b': {'name': 'Application', 'children': ['29380f60-620e-4314-9969-4ad6fe5bbea6'], 'id': '86361ab4-6002-4f3b-b6ca-7e35acd69f9b', 'properties': {}}, '223d9749-feb2-425d-b512-17b5322cda96': {'name': 'S_Group', 'children': ['86361ab4-6002-4f3b-b6ca-7e35acd69f9b'], 'id': '223d9749-feb2-425d-b512-17b5322cda96', 'properties': {}}}
Transitions: {'c4e2026a-9c57-4bb0-b2e2-f7068d9c6fe5': {'source': '29380f60-620e-4314-9969-4ad6fe5bbea6', 'target': '040677a5-820f-4d17-ae50-1296c0e36273', 'id': 'c4e2026a-9c57-4bb0-b2e2-f7068d9c6fe5', 'properties': {}}}

相关问题