다른 키 값의 길이와 동일한 값을 사용하여 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 개체(JSON 라인파일), 배열이 아닌 각 줄에 개체가 있기 때문에 선행이 .[]올바르지 않습니다 ..

당신은 간단한 것을 원한다

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.

관련 정보