将新密钥添加到 JSON 文件,其值等于其他密钥值的长度

将新密钥添加到 JSON 文件,其值等于其他密钥值的长度

我正在调整一个包含大量对象的 JSON 文件。以下是前三个对象的示例:

{"id":"0704.0001","submitter":"Pavel Nadolsky","authors":"C. Bal\\'azs, E. L. Berger, P. M. Nadolsky, C.-P. Yuan","title":"Calculation of prompt diphoton production cross sections at Tevatron and\n  LHC energies","comments":"37 pages, 15 figures; published version","journal-ref":"Phys.Rev.D76:013009,2007","doi":"10.1103/PhysRevD.76.013009","report-no":"ANL-HEP-PR-07-12","categories":"hep-ph","license":null,"abstract":"  A fully differential calculation in perturbative quantum chromodynamics is\npresented for the production of massive photon pairs at hadron colliders. All\nnext-to-leading order perturbative contributions from quark-antiquark,\ngluon-(anti)quark, and gluon-gluon subprocesses are included, as well as\nall-orders resummation of initial-state gluon radiation valid at\nnext-to-next-to-leading logarithmic accuracy. The region of phase space is\nspecified in which the calculation is most reliable. Good agreement is\ndemonstrated with data from the Fermilab Tevatron, and predictions are made for\nmore detailed tests with CDF and DO data. Predictions are shown for\ndistributions of diphoton pairs produced at the energy of the Large Hadron\nCollider (LHC). Distributions of the diphoton pairs from the decay of a Higgs\nboson are contrasted with those produced from QCD processes at the LHC, showing\nthat enhanced sensitivity to the signal can be obtained with judicious\nselection of events.\n","versions":[{"version":"v1","created":"Mon, 2 Apr 2007 19:18:42 GMT"},{"version":"v2","created":"Tue, 24 Jul 2007 20:10:27 GMT"}],"update_date":"2008-11-26","authors_parsed":[["Bal\u00e1zs","C.",""],["Berger","E. L.",""],["Nadolsky","P. M.",""],["Yuan","C. -P.",""]]}
{"id":"0704.0002","submitter":"Louis Theran","authors":"Ileana Streinu and Louis Theran","title":"Sparsity-certifying Graph Decompositions","comments":"To appear in Graphs and Combinatorics","journal-ref":null,"doi":null,"report-no":null,"categories":"math.CO cs.CG","license":"http://arxiv.org/licenses/nonexclusive-distrib/1.0/","abstract":"  We describe a new algorithm, the $(k,\\ell)$-pebble game with colors, and use\nit obtain a characterization of the family of $(k,\\ell)$-sparse graphs and\nalgorithmic solutions to a family of problems concerning tree decompositions of\ngraphs. Special instances of sparse graphs appear in rigidity theory and have\nreceived increased attention in recent years. In particular, our colored\npebbles generalize and strengthen the previous results of Lee and Streinu and\ngive a new proof of the Tutte-Nash-Williams characterization of arboricity. We\nalso present a new decomposition that certifies sparsity based on the\n$(k,\\ell)$-pebble game with colors. Our work also exposes connections between\npebble game algorithms and previous sparse graph algorithms by Gabow, Gabow and\nWestermann and Hendrickson.\n","versions":[{"version":"v1","created":"Sat, 31 Mar 2007 02:26:18 GMT"},{"version":"v2","created":"Sat, 13 Dec 2008 17:26:00 GMT"}],"update_date":"2008-12-13","authors_parsed":[["Streinu","Ileana",""],["Theran","Louis",""]]}
{"id":"0704.0003","submitter":"Hongjun Pan","authors":"Hongjun Pan","title":"The evolution of the Earth-Moon system based on the dark matter field\n  fluid model","comments":"23 pages, 3 figures","journal-ref":null,"doi":null,"report-no":null,"categories":"physics.gen-ph","license":null,"abstract":"  The evolution of Earth-Moon system is described by the dark matter field\nfluid model proposed in the Meeting of Division of Particle and Field 2004,\nAmerican Physical Society. The current behavior of the Earth-Moon system agrees\nwith this model very well and the general pattern of the evolution of the\nMoon-Earth system described by this model agrees with geological and fossil\nevidence. The closest distance of the Moon to Earth was about 259000 km at 4.5\nbillion years ago, which is far beyond the Roche's limit. The result suggests\nthat the tidal friction may not be the primary cause for the evolution of the\nEarth-Moon system. The average dark matter field fluid constant derived from\nEarth-Moon system data is 4.39 x 10^(-22) s^(-1)m^(-1). This model predicts\nthat the Mars's rotation is also slowing with the angular acceleration rate\nabout -4.38 x 10^(-22) rad s^(-2).\n","versions":[{"version":"v1","created":"Sun, 1 Apr 2007 20:46:54 GMT"},{"version":"v2","created":"Sat, 8 Dec 2007 23:47:24 GMT"},{"version":"v3","created":"Sun, 13 Jan 2008 00:36:28 GMT"}],"update_date":"2008-01-13","authors_parsed":[["Pan","Hongjun",""]]}

我正在尝试添加一个调用title_length每个对象的新键。该键的值应该是键值的长度title

到目前为止,我已经尝试过

jq '.[] | .title_length = (.title | length)' file.json > new_file.json

但这给了我以下错误:

jq: error (at file.json:74850): Cannot index string with string "title"

我不明白这个错误,而且我似乎无法绕过它。

答案1

如果文件的每一行都包含一个 JSON 对象(ajson行文件),那么前导.[]不正确,因为.将是每行的对象,而不是数组。

你想要简单

jq -c '.title_length = (.title | length)'

或者

jq -c '. + {title_length: .title | length}'

请注意-c保持输出像输入一样压缩的选项。

答案2

格伦已经诊断出这个问题(您有一组对象,而不是数组)并为您提供了正确的jq表达式。

我只是要展示使用另一种工具做同样事情的另一种方法,磨坊主( mlr),用于处理结构化数据的通用实用程序。

mlr --jsonl put '$title_length = strlen($title)' file

这会将文件读取为 JSONL 数据集(JSON 对象的集合,每行一个)。对于每个此类对象,它都会创建一个新字段 ,title_length其值计算为对象title字段的字符串长度。输出将在标准输出上生成为 JSONL。

这种解决任务的方式可能会让用户感觉比较熟悉awk,或者让人不熟悉jq

相关内容