首页 \ 问答 \ 什么是JSON的好的CLI工具?(What are good CLI tools for JSON?)

什么是JSON的好的CLI工具?(What are good CLI tools for JSON?)

一般问题

虽然我可能正在诊断事件的根本原因,确定其影响的用户数量,或者蒸馏计时日志,以评估最近代码更改的性能和吞吐量影响,但我的工具保持不变: grepawksedtruniqsortzcattailheadjoinsplit 。 为了将它们粘合在一起,Unix给了我们管道,而对于鸽友过滤我们有xargs 。 如果这些让我失望了,总是会有这样的。

这些工具非常适用于处理CSV文件,制表符分隔文件,具有可预测行格式的日志文件,或以逗号分隔的键值对的文件。 换句话说,每一行都没有上下文的文件。

XML类似物

我最近需要通过千兆字节的XML来构建用户使用的直方图。 这是很简单的工具,我有,但对于更复杂的查询,正常的方法分解。 说我有这样的项目的文件:

<foo user="me">
    <baz key="zoidberg" value="squid" />
    <baz key="leela"    value="cyclops" />
    <baz key="fry"      value="rube" />
</foo>

而且我想说,我想要生成一个从<foo>用户到平均值<baz>的映射。 逐行处理不再是一个选择:我需要知道我正在检查哪个用户的<foo>所以我知道要更新的平均值。 任何一种完成这项任务的Unix一个班轮都可能是不可思议的。

幸运的是,在XML-land中,我们有很好的技术,如XPath,XQuery和XSLT来帮助我们。

以前,我习惯使用精彩的XML::XPath Perl模块来完成上述的查询,但是在找到可以对我当前窗口运行XPath表达式的TextMate插件后,我停止写一次性Perl脚本查询XML。 而且我刚刚发现了关于正在安装的XMLStarlet ,当我输入这个,我期望在将来使用它。

JSON解决方案?

所以这引起了我的问题:JSON有什么工具吗? 这只是一个时间问题,一些调查任务要求我对JSON文件进行类似的查询,而没有像XPath和XSLT这样的工具,这样的任务会变得更加困难。 如果我有一堆像这样的JSON:

{
  "firstName": "Bender",
  "lastName": "Robot",
  "age": 200,
  "address": {
    "streetAddress": "123",
    "city": "New York",
    "state": "NY",
    "postalCode": "1729"
  },
  "phoneNumber": [
    { "type": "home", "number": "666 555-1234" },
    { "type": "fax", "number": "666 555-4567" }
  ]
}

并且想要找到每个人的平均电话号码,我可以用XPath做这样的事情:

fn:avg(/fn:count(phoneNumber))

问题

  1. 有没有任何可以以这种方式“查询”JSON文件的命令行工具?
  2. 如果您必须在Unix命令行上处理一堆JSON文件,您使用什么工具?
  3. 哎呀,有没有为JSON做这样的查询语言呢?
  4. 如果你在日常工作中使用这样的工具,你喜欢/不喜欢什么呢? 有什么问题吗?

我注意到越来越多的数据序列化正在使用JSON,因此在分析大量数据转储时,这样的处理工具将是至关重要的。 用于JSON的语言库非常强大,编写脚本来进行这种处理是非常简单的,但是真正让人们玩数据的工具需要使用。

相关问题

  • Grep和Sed等效于XML命令行处理
  • JSON有查询语言吗?
  • JSONPath或其他XPath类似于JSON / Javascript的实用程序; 或Jquery JSON

General Problem

Though I may be diagnosing the root cause of an event, determining how many users it affected, or distilling timing logs in order to assess the performance and throughput impact of a recent code change, my tools stay the same: grep, awk, sed, tr, uniq, sort, zcat, tail, head, join, and split. To glue them all together, Unix gives us pipes, and for fancier filtering we have xargs. If these fail me, there's always perl -e.

These tools are perfect for processing CSV files, tab-delimited files, log files with a predictable line format, or files with comma-separated key-value pairs. In other words, files where each line has next to no context.

XML Analogues

I recently needed to trawl through Gigabytes of XML to build a histogram of usage by user. This was easy enough with the tools I had, but for more complicated queries the normal approaches break down. Say I have files with items like this:

<foo user="me">
    <baz key="zoidberg" value="squid" />
    <baz key="leela"    value="cyclops" />
    <baz key="fry"      value="rube" />
</foo>

And let's say I want to produce a mapping from user to average number of <baz>s per <foo>. Processing line-by-line is no longer an option: I need to know which user's <foo> I'm currently inspecting so I know whose average to update. Any sort of Unix one liner that accomplishes this task is likely to be inscrutable.

Fortunately in XML-land, we have wonderful technologies like XPath, XQuery, and XSLT to help us.

Previously, I had gotten accustomed to using the wonderful XML::XPath Perl module to accomplish queries like the one above, but after finding a TextMate Plugin that could run an XPath expression against my current window, I stopped writing one-off Perl scripts to query XML. And I just found out about XMLStarlet which is installing as I type this and which I look forward to using in the future.

JSON Solutions?

So this leads me to my question: are there any tools like this for JSON? It's only a matter of time before some investigation task requires me to do similar queries on JSON files, and without tools like XPath and XSLT, such a task will be a lot harder. If I had a bunch of JSON that looked like this:

{
  "firstName": "Bender",
  "lastName": "Robot",
  "age": 200,
  "address": {
    "streetAddress": "123",
    "city": "New York",
    "state": "NY",
    "postalCode": "1729"
  },
  "phoneNumber": [
    { "type": "home", "number": "666 555-1234" },
    { "type": "fax", "number": "666 555-4567" }
  ]
}

And wanted to find the average number of phone numbers each person had, I could do something like this with XPath:

fn:avg(/fn:count(phoneNumber))

Questions

  1. Are there any command-line tools that can "query" JSON files in this way?
  2. If you have to process a bunch of JSON files on a Unix command line, what tools do you use?
  3. Heck, is there even work being done to make a query language like this for JSON?
  4. If you do use tools like this in your day-to-day work, what do you like/dislike about them? Are there any gotchas?

I'm noticing more and more data serialization is being done using JSON, so processing tools like this will be crucial when analyzing large data dumps in the future. Language libraries for JSON are very strong and it's easy enough to write scripts to do this sort of processing, but to really let people play around with the data shell tools are needed.

Related Questions


原文:https://stackoverflow.com/questions/2933126
更新时间:2022-06-07 20:06

最满意答案

你应该做

<%= f.hidden_field :speaker_id, :value => @speaker.id %>

这将生成嵌套在event_speaker中的speaker_id,因此您的表单将如下所示:

<%= form_for(@event_speaker) do |f| %>
  <% if @event_speaker.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@event_speaker.errors.count, "error") %> prohibited this event_speaker from being saved:</h2>
      <ul>
        <% @event_speaker.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p><%= @speaker.first_name %></p>
  <div class="actions">
    <%= f.hidden_field :speaker_id, :value => @speaker.id %>
  </div>
  <div class="field">
    <%= f.label :event %><br>
    <%= f.collection_select(:event_id, @upcoming_events, :id, :name)%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

有关详细信息,请检查rails中的hidden field


You should do

<%= f.hidden_field :speaker_id, :value => @speaker.id %>

This will generate your speaker_id nested inside event_speaker so your form will look like this:

<%= form_for(@event_speaker) do |f| %>
  <% if @event_speaker.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@event_speaker.errors.count, "error") %> prohibited this event_speaker from being saved:</h2>
      <ul>
        <% @event_speaker.errors.full_messages.each do |msg| %>
          <li><%= msg %></li>
        <% end %>
      </ul>
    </div>
  <% end %>

  <p><%= @speaker.first_name %></p>
  <div class="actions">
    <%= f.hidden_field :speaker_id, :value => @speaker.id %>
  </div>
  <div class="field">
    <%= f.label :event %><br>
    <%= f.collection_select(:event_id, @upcoming_events, :id, :name)%>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

For details checkout hidden field in rails

相关问答

更多
  • 你应该做 <%= f.hidden_field :speaker_id, :value => @speaker.id %> 这将生成嵌套在event_speaker中的speaker_id,因此您的表单将如下所示: <%= form_for(@event_speaker) do |f| %> <% if @event_speaker.errors.any? %>

    <%= pluralize(@event_speake ...

  • 我认为没有内置任何东西。有了这个说法,你可以很容易地制作自己的自定义HTML Helper类,你可以将所有必需的隐藏字段作为字符串甚至是模型属性传递给它,它将创建所有为你隐藏的领域。 我找到了一个关于如何做到这一点的教程(请在下面找到),但我相信你可以通过谷歌搜索custom html helper mvc tutorial或类似的东西找到更多。 希望这可以帮助 :) http://dailydotnettips.com/2011/03/14/creating-custom-html-helpers-in- ...
  • 您需要将隐藏的下拉菜单设置为禁用。 “已禁用元素”属性未提交,或者您可以说其值未发布。 即 disabled="disabled" FYI 禁用的控件无法获得焦点。 在选项卡导航中跳过禁用的控件。 禁用的控件无法成功发布。 希望这可以帮助。 You need to set the hidden drop-downs to be disabled. Elements with Disabled attribute are not submitted or you can say their value ...
  • 视图中的代码是否在new视图中? 我不确定你要做什么,但尝试在create动作中加入@text = params[:text] - 如果你正在加载新动作, params[:text]在加载时将为空白 the code in your view is in the new view? I'm not sure what you're trying to do, but try putting @text = params[:text] in the create action - if you're load ...
  • 在大多数情况下,它被认为是非常差的做法。 相反,通常最好在检查条件后在服务器端将按钮呈现为禁用。 还要确保清理此表单发出的请求(虽然您的条件不允许,但单击按钮)。 用户可以取消禁用该按钮并单击它。 如果您发现由于某种原因必须以HTML格式保存一些信息,我会使用数据 - * - 属性! 希望有所帮助! 编辑伪代码示例: # MyView.html.erb <% unless @is_draft %> <%= render partial: 'my_checkbox_button_enabling', f ...
  • 您可以使用隐藏字段发布数据。 但是,如果您希望用户不能读取隐藏字段中设置的值,则可以在隐藏字段中设置之前加密该值,然后进行设置。 通过这种方式,除了你之外,没有人知道如何解码该值,就能读出它。 希望这可以帮助。 You can use hidden fields for posting data. But if you want that the user should not be able to read the values set in hidden field, you can encrypt t ...
  • 您需要将您的字段的名称更改为user_id以使其更传统。 您可以使用params散列来访问任何POST / GET数据。 # your controller params[:user_id] # => "field_value You need to change the name of your field to user_id to be more traditional. You access any POST/GET data with the params hash. # your contro ...
  • 您的隐藏字段参数不会进入正确位置的参数哈希,因为您使用了hidden_field_tag并且未指定它们将包含在哈希中的answer键下。 尝试这个: <%= hidden_field_tag "answer[user_id]", @user.id %> Your hidden field parameters aren't going in to the parameters hash in the right spot because you used hidden_field_tag and didn ...
  • Standej大多是正确的,只需修改他的答案如下: $("#addedRows p").find("input[type=hidden], input[type=text]").each(function(){ Standej is mostly right , just modify his answer as following: $("#addedRows p").find("input[type=hidden], input[type=text]").each(function(){
  • UPDATE 在控制器中分配lang解决方案: def create @article = Article.new(article_params) @article.lang = params[:title].blank? ? "de" : "eng" if @article.save redirect_to @article else render 'new' end end 或者,如果您愿意,可以使用javascript / jquery获取:title的值,然 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)