将JSON文件拆分为单独的文件

时间:2023-01-22 21:10:47

I have a large JSON file that is an object of objects, which I would like to split into separate files name after object keys. Is it possible to achieve this using jq or any other off-the-shelf tools?

我有一个大型的JSON文件,它是一个对象的对象,我想在对象键之后拆分成单独的文件名。是否可以使用jq或任何其他现成的工具来实现这一目标?

The original JSON is in the following format

原始JSON采用以下格式

{ "item1": {...}, "item2": {...}, ...}

{“item1”:{...},“item2”:{...},...}

Given this input I would like to produce files item1.json, item2.json etc.

鉴于此输入,我想生成文件item1.json,item2.json等。

2 个解决方案

#1


8  

This should give you a start:

这应该给你一个开始:

for f in `cat input.json | jq -r 'keys[]'` ; do
  cat input.json | jq ".$f" > $f.json
done

or when you insist on more bashy syntax like some seem to prefer:

或者当你坚持使用更像一些似乎更喜欢的bashy语法:

for f in $(jq -r 'keys[]') ; do
  jq ".[\"$f\"]" < input.json > "$f.json"
done < input.json

#2


3  

Here's a solution that requires only one call to jq:

这是一个只需要一次调用jq的解决方案:

jq -cr 'keys[] as $k | "\($k)\n\(.[$k])"' input.json |
  while read -r key ; do
    read -r item
    printf "%s\n" "$item" > "/tmp/$key.json"
  done

It might be faster to pipe the output of the jq command to awk, e.g.:

将jq命令的输出传递给awk可能会更快,例如:

jq -cr 'keys[] as $k | "\($k)\t\(.[$k])"' input.json |
  awk -F\\t '{ print $2 > "/tmp/" $1 ".json" }'

Of course, these approaches will need to be modified if the key names contain characters that cannot be used in filenames.

当然,如果键名包含不能在文件名中使用的字符,则需要修改这些方法。

#1


8  

This should give you a start:

这应该给你一个开始:

for f in `cat input.json | jq -r 'keys[]'` ; do
  cat input.json | jq ".$f" > $f.json
done

or when you insist on more bashy syntax like some seem to prefer:

或者当你坚持使用更像一些似乎更喜欢的bashy语法:

for f in $(jq -r 'keys[]') ; do
  jq ".[\"$f\"]" < input.json > "$f.json"
done < input.json

#2


3  

Here's a solution that requires only one call to jq:

这是一个只需要一次调用jq的解决方案:

jq -cr 'keys[] as $k | "\($k)\n\(.[$k])"' input.json |
  while read -r key ; do
    read -r item
    printf "%s\n" "$item" > "/tmp/$key.json"
  done

It might be faster to pipe the output of the jq command to awk, e.g.:

将jq命令的输出传递给awk可能会更快,例如:

jq -cr 'keys[] as $k | "\($k)\t\(.[$k])"' input.json |
  awk -F\\t '{ print $2 > "/tmp/" $1 ".json" }'

Of course, these approaches will need to be modified if the key names contain characters that cannot be used in filenames.

当然,如果键名包含不能在文件名中使用的字符,则需要修改这些方法。