如何在 Ubuntu 上查看打印的大 JSON 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10704178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:21:02  来源:igfitidea点击:

How to see a large JSON file pretty printed on Ubuntu?

jsonubuntusyntax-highlightingpretty-print

提问by JJD

I would like to hear your suggestions on how to handle a large (40MB) JSON file on Ubuntu. I would like to see it pretty printed in vimor geditor any other editor. One can find numerious tutorials on how to prettify the JSON, however, they do not have to deal with large input. I also imagine I could pipe the data through pygmentsor any other syntax highlighter. I am curious to hearing your ideas.

我想听听您关于如何在 Ubuntu 上处理大型 (40MB) JSON 文件的建议。我希望在vimgedit或任何其他编辑器中看到它的漂亮打印。可以找到大量有关如何美化 JSON 的教程,但是,它们不必处理大量输入。我还想象我可以通过pygments或任何其他语法高亮器来传输数据。我很想听听你的想法。

Example download:

示例下载:

wget -O large-dataset.json http://data.wien.gv.at/daten/wfs?service=WFS&request=GetFeature&version=1.1.0&typeName=ogdwien:BAUMOGD&srsName=EPSG:4326&outputFormat=json

Please mind the download size!

请注意下载大小!



EDIT:I found out that meldis working somewhat. The application does not load the whole file at once which would block the user interface. Instead it sequentially reads the file content.

编辑:我发现meld有点工作。应用程序不会一次加载整个文件,这会阻塞用户界面。相反,它顺序读取文件内容。

回答by gertvdijk

Python JSON's module can do this too (python -m json.tool), e.g.:

Python JSON 的模块也可以这样做 ( python -m json.tool),例如:

cat myjsonfile.json | python -m json.tool > pretty.json

回答by toto_tico

If you just want to visualize (and search)a json file, Firefoxdoes a pretty good job. I don't have a 40MBfile on hand, but it easily handled a 9MBone.

如果你只是想可视化(和搜索)一个 json 文件,Firefox做得很好。我手头没有40MB文件,但它可以轻松处理9MB的文件。

Just drag the JSON file to Firefox, or run:

只需将 JSON 文件拖到 Firefox,或运行:

firefox your_file.json

firefox your_file.json

回答by sagar buddhi

jq is a lightweight commandline JSON processor and works well! for this you need to install jq with the command below(if you are using apt package manager).

jq 是一个轻量级的命令行 JSON 处理器,运行良好!为此,您需要使用以下命令安装 jq(如果您使用的是 apt 包管理器)。

sudo apt-get install jq

Below command will pretty json to a new file.

下面的命令将漂亮的 json 转换为一个新文件。

 jq '.' non-pretty.json > pretty.json

Also we can filter the json with jq, I found it to be very helpful while working with large geojson files, for instance below command will save only properties of first feature.

我们也可以用 jq 过滤 json,我发现它在处理大型 geojson 文件时非常有用,例如下面的命令将只保存第一个特征的属性。

jq '.features[0].properties' geojson_file.json > pretty.json

Hope this will be helpful!

希望这会有所帮助!

回答by Rodrigo Asensio

Do you have KDE or any other visual environment? If yes, have you tried using the chrome extension JSONView?

你有 KDE 或任何其他可视化环境吗?如果是,您是否尝试过使用 Chrome 扩展JSONView

回答by Sairam Krish

The core usage is pretty formatting large json. I tested Chrome extension JSON View with 25MB json file. It crashes on loading this as a local file or from network. By crash, I mean JSON will not get formatted and on looking into JSON view options, you will get a crash message. I also tried similar addons for firefox. I tried online json formatters as well.

核心用法是格式化大 json。我使用 25MB json 文件测试了 Chrome 扩展 JSON 视图。它在将此作为本地文件或从网络加载时崩溃。通过崩溃,我的意思是 JSON 不会被格式化,并且在查看 JSON 视图选项时,您将收到一条崩溃消息。我也尝试过类似的 Firefox 插件。我也尝试过在线 json 格式化程序。

Found this library - jsonpps. Works pretty well to pretty format large json from command line, taking input and saving the formatted json as separate file. It can also save in the same file (need optional parameter)

找到了这个库 - jsonpps。从命令行漂亮地格式化大型 json 非常有效,接受输入并将格式化的 json 保存为单独的文件。它也可以保存在同一个文件中(需要可选参数)

One drawback, To install and run, one should be familier with Java and Maven.

缺点之一,要安装和运行,必须熟悉Java和Maven。

To install & run:

安装和运行:

git clone https://github.com/bazaarvoice/jsonpps.git
mvn clean package
cd target 
java -jar jsonpps-1.2-SNAPSHOT.jar -o /path/to/output.json /path/to/largeInput.json

This solution is not restricted to Ubuntu. It should work on any operating system.

此解决方案不限于 Ubuntu。它应该适用于任何操作系统。

回答by Divyanshu Srivastava

I usually use Sublime Textfor this purpose. There is a dedicated plugin for this job.

为此,我通常使用Sublime Text。这项工作有一个专用的插件。

The plugin Pretty JSONparses the JSON contents selected, and prints them in a structured way.

插件Pretty JSON解析选定的 JSON 内容,并以结构化的方式打印它们。

All you need to do is to select the contents and press Ctrl+Alt+j.

您需要做的就是选择内容并按Ctrl+ Alt+ j