合并特定文件夹中的所有JSON文件

时间:2023-01-14 10:27:46

I have a folder with a number of JSON files within it.

我有一个包含许多JSON文件的文件夹。

I want to take all .json files, where the filename ends with _LAST.json, and combine them into one JSON file. Each of the files that are to be processed are named like:

我想获取所有.json文件,其中文件名以_LAST.json结尾,并将它们合并为一个JSON文件。要处理的每个文件都命名为:

Bill_LAST.json
Jane_LAST.json
Kitty_LAST.json

Bill_LAST.json looks like:

Bill_LAST.json看起来像:

[{

    "Date": "2012-11-19",
    "Name": "Bill",
    "Age": 54,
    "Sex": "M"

}]

1 个解决方案

#1


5  

It could be as simple as:

它可以很简单:

json = Dir['./*_LAST.json'].map { |f| JSON.parse File.read(f) }.flatten

Here is a working demo.

#1


5  

It could be as simple as:

它可以很简单:

json = Dir['./*_LAST.json'].map { |f| JSON.parse File.read(f) }.flatten

Here is a working demo.