将JSON文件转换为表格格式的CSV文件。

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

Transform JSON file to table format CSV file

问题

以下是您要翻译的内容:

来自 Oracle Service Cloud Right Now API 的导出分析报告(参考链接)是一个具有以下格式的 JSON 文件:

{
	"count": 2,
	"name": "filename",
	"columnNames": [
		"Name1",
		"Name2",
		"Name3",
		"Name4",
		"Name5"
	],
	"rows": [
		[
			"ValueColumn1Row1",
			"ValueColumn2Row1",
			"ValueColumn3Row1",
			"ValueColumn4Row1",
			"ValueColumn5Row1"
		],
		[
			"ValueColumn1Row2",
			"ValueColumn2Row2",
			"ValueColumn3Row2",
			"ValueColumn4Row2",
			"ValueColumn5Row2"
		]
	],
	"links": [
		{
			"rel": "self",
			"href": "https://somecompany.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"
		}
	]
}

但为了处理数据,我需要将报告转换为 CSV 表格格式。这个示例报告有五列和两行值。

Name1 Name2 Name3 Name4 Name5
ValueColumn1Row1 ValueColumn2Row1 ValueColumn3Row1 ValueColumn4Row1 ValueColumn5Row1
ValueColumn1Row2 ValueColumn2Row2 ValueColumn3Row2 ValueColumn4Row2 ValueColumn5Row2

我尝试了 Power Query 和不同的在线转换工具,但它们似乎无法识别列名作为标题,并将相应的值放入列中,除了 "links" 部分。

用人类语言来定义转换过程,可以像这样:

  1. 从 "columnNames" 中定义列名。
  2. 从 "rows" 中读取值(每五行为一项)。
  3. 将每个值放入相应的列中。
  4. 将结果保存为 CSV 文件。

但是我的编程技能非常基础。我希望有人能为我提供如何操作的详细指导。

Kind Regards

英文:

An exported Analytics Report from Oracle Service Cloud Right Now API (reference) is a Json file with the following format:

{
	"count": 2,
	"name":	"filename",
	"columnNames": [
		    "Name1",
        	"Name2",
        	"Name3",
        	"Name4",
        	"Name5"
	],
	"rows": 
	[
		[
	     "ValueColumn1Row1",
         "ValueColumn2Row1",
         "ValueColumn3Row1",
         "ValueColumn4Row1",
         "ValueColumn5Row1"
		],
		[
		 "ValueColumn1Row2",
         "ValueColumn2Row2",
         "ValueColumn3Row2",
         "ValueColumn4Row2",
         "ValueColumn5Row2"
		]
	],
	"links": 
	[
		{
		"rel": "self",
		"href": "https://somecompany.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"
		}
	]
}

But in order to process the data I need a table format in csv. The report in this example has five columns with two rows of values.

Name1 Name2 Name3 Name4 Name5
ValueColumn1Row1 ValueColumn2Row1 ValueColumn3Row1 ValueColumn4Row1 ValueColumn5Row1
ValueColumn1Row2 ValueColumn2Row2 ValueColumn3Row2 ValueColumn4Row2 ValueColumn5Row2

I tried Powery Query and different online converters. But they don't seem to identify the column names as headers and set the corresponding values in the columns. Except for the "links" section.

In human language I can define the transform process something like this:

  1. Define the columnnames from "columnNames"
  2. Read the values from "rows" (every five rows per item)
  3. Set every value in the corresponding column
  4. Save the result as a CSV file

But my programming skills are very very basic. I hope someone can shed some light on how to do is.

Kind Regards

答案1

得分: 1

你可以轻松地通过使用Python和pandas库来实现这个目标。

安装:

pip install pandas

然后创建一个Python文件,其中包含以下内容:

import pandas as pd
import json

# 假设json_data是你加载的JSON数据

json_data = {
    "count": 2,
    "name": "filename",
    "columnNames": [
        "Name1",
        "Name2",
        "Name3",
        "Name4",
        "Name5"
    ],
    "rows": [
        [
            "ValueColumn1Row1",
            "ValueColumn2Row1",
            "ValueColumn3Row1",
            "ValueColumn4Row1",
            "ValueColumn5Row1"
        ],
        [
            "ValueColumn1Row2",
            "ValueColumn2Row2",
            "ValueColumn3Row2",
            "ValueColumn4Row2",
            "ValueColumn5Row2"
        ]
    ],
    "links": [
        {
            "rel": "self",
            "href": "https://somecompany.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"
        }
    ]
}

# 将JSON转换为DataFrame
df = pd.DataFrame(json_data['rows'], columns=json_data['columnNames'])

# 将DataFrame写入CSV文件
df.to_csv('output.csv', index=False)

脚本说明:

  1. 导入必要的库(pandas和json)。
  2. 读取JSON数据。在这种情况下,我在脚本中直接定义了JSON数据。在你的情况下,你可能会从文件中读取它。
  3. 将JSON数据转换为pandas DataFrame。这是pandas提供的一个类似于数据库表的表格数据结构。
  4. 将DataFrame写入CSV文件。使用index=False参数来防止pandas写入行号。
英文:

You can easily achieve that with the help of python and the pandas library

To install:

pip install pandas

then create a python file with the following content:

import pandas as pd
import json

# assuming json_data is your loaded json data

json_data = {
    "count": 2,
    "name": "filename",
    "columnNames": [
            "Name1",
            "Name2",
            "Name3",
            "Name4",
            "Name5"
    ],
    "rows": [
        [
         "ValueColumn1Row1",
         "ValueColumn2Row1",
         "ValueColumn3Row1",
         "ValueColumn4Row1",
         "ValueColumn5Row1"
        ],
        [
         "ValueColumn1Row2",
         "ValueColumn2Row2",
         "ValueColumn3Row2",
         "ValueColumn4Row2",
         "ValueColumn5Row2"
        ]
    ],
    "links": [
        {
        "rel": "self",
        "href": "https://somecompany.custhelp.com/services/rest/connect/v1.4/analyticsReportResults"
        }
    ]
}

# Converting json to dataframe
df = pd.DataFrame(json_data['rows'], columns=json_data['columnNames'])

# Write DataFrame to CSV
df.to_csv('output.csv', index=False)

Explanation of the script:

  1. Importing the necessary libraries (pandas and json).
  2. Reading the JSON data. In this case, I have defined the JSON data directly in the script. In your case, you might read it from a file.
  3. Converting the JSON data into a pandas DataFrame. This is a tabular data structure that pandas provides which is very similar to a table in a database.
  4. Writing the DataFrame to a CSV file. The index=False parameter is used to prevent pandas from writing row numbers.

huangapple
  • 本文由 发表于 2023年7月17日 16:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702796.html
匿名

发表评论

匿名网友

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

确定