我正在调整一个包含大量对象的 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"
我不明白这个错误,而且我似乎无法绕过它。
如果文件的每一行都包含一个 JSON 对象(一个jsonlines文件),那么前导
.[]
是不正确的,因为.
将是每一行的对象,而不是数组。你想要的只是
或者
请注意
-c
保持输出像输入一样压缩的选项。glenn 已经诊断出这个问题(你有一组对象,而不是数组)并给了你一个更正的
jq
表达式。我将展示使用另一种工具 Miller ( ) 来做同样事情的替代方法,Miller (
mlr
) 是一种用于处理结构化数据的通用实用程序。这会将文件读取为 JSONL 数据集(一组 JSON 对象,每行一个)。对于每个这样的对象,它都会创建一个新字段 ,
title_length
其值计算为对象title
字段的字符串长度。输出将作为 JSONL 在标准输出上生成。这种解决任务的方式对于 的用户来说可能比较熟悉
awk
,也可能让不熟悉的人感觉更熟悉jq
。