{"id":24342,"date":"2022-12-02T06:46:03","date_gmt":"2022-12-02T01:16:03","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/"},"modified":"2022-12-02T06:46:03","modified_gmt":"2022-12-02T01:16:03","slug":"solved-finding-the-location-line-column-of-a-field-value-in-a-json-file","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/","title":{"rendered":"[Solved] Finding the location (line, column) of a field value in a JSON file"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-73644612\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"73644612\" data-parentid=\"73616608\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>Given JSON data and a query, there is no<\/p>\n<blockquote>\n<p>option in <code>jq<\/code> that, instead of printing the value itself, prints the location<\/p>\n<\/blockquote>\n<p>of possible matches.<\/p>\n<p>This is because JSON parsers providing an interface to developers usually focus on processing the logical structure of a JSON input, not the textual stream conveying it. You would have to instruct it to explicitly treat its input as raw text, while properly parsing it at the same time in order to extract the queried value. In the case of <code>jq<\/code>, the former can be achieved using the <code>--raw-input<\/code> (or <code>-R<\/code>) option, the latter then by parsing the read-in JSON-encoded string using <code>fromjson<\/code>.<\/p>\n<p>The <code>-R<\/code> option alone would read the input linewise into an array of strings, which would have to be concatenated (e.g. using <code>add<\/code>) in order to provide the whole input at once to <code>fromjson<\/code>. The other way round, you could also provide the <code>--slurp<\/code> (or <code>-s<\/code>) option which (in combination with <code>-R<\/code>) already concatenates the input to a single string which then, after having parsed it with <code>fromjson<\/code>, would have to be split again into lines (e.g. using <code>\/\"\\n\"<\/code>) in order to provide row numbers. I found the latter to be more convenient.<\/p>\n<p>That said, this could give you a starting point (the <code>--raw-output<\/code> (or <code>-r<\/code>) option outputs raw text instead of JSON):<\/p>\n<pre class=\"lang-bash prettyprint-override\"><code>jq -Rrs '\n  \"\\(fromjson.key2.key2_2.key2_2_1)\" as $query    # save the query value as string\n  | ($query | length) as $length      # save its length by counting its characters\n  | .\/\"\\n\" | to_entries[]      # split into lines and provide 0-based line numbers\n  | {row: .key, col: .value | indices($query)[]}   # find occurrences of the query\n  | \"(\\(.row),\\(.col)) (\\(.row),\\(.col + $length))\"            # format the output\n'\n<\/code><\/pre>\n<pre class=\"lang-json prettyprint-override\"><code>(5,24) (5,34)\n<\/code><\/pre>\n<p><a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/jqplay.org\/s\/omxgd95n3op\">Demo<\/a><\/p>\n<p>Now, this works for the sample query, how about the general case? Your example queried a number (<code>1.43123123<\/code>) which is an easy target as it has the same textual representation when encoded as JSON. Therefore, a simple string search and length count did a fairly good job (not a perfect one because it would still find any occurrence of that character stream, not just &#8220;values&#8221;). Thus, for more precision, but especially with more complex JSON datatypes being queried, you would need to develop a more sophisticated searching approach, probably involving more JSON conversions, whitespace stripping and other normalizing shenanigans. So, unless your goal is to rebuild a full JSON parser within another one, you should narrow it down to the kind of queries you expect, and compose an appropriately tailored searching approach. This solution provides you with concepts to simultaneously process the input textually and structurally, and with a simple search and ouput integration.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Finding the location (line, column) of a field value in a JSON file <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Given JSON data and a query, there is no option in jq that, instead of printing the value itself, prints the location of possible matches. This is because JSON parsers providing an interface to developers usually focus on processing the logical structure of a JSON input, not the textual stream conveying it. You would &#8230; <a title=\"[Solved] Finding the location (line, column) of a field value in a JSON file\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\" aria-label=\"More on [Solved] Finding the location (line, column) of a field value in a JSON file\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1917,356],"class_list":["post-24342","post","type-post","status-publish","format-standard","hentry","category-solved","tag-jq","tag-json"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Finding the location (line, column) of a field value in a JSON file - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Finding the location (line, column) of a field value in a JSON file - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Given JSON data and a query, there is no option in jq that, instead of printing the value itself, prints the location of possible matches. This is because JSON parsers providing an interface to developers usually focus on processing the logical structure of a JSON input, not the textual stream conveying it. You would ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-02T01:16:03+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Finding the location (line, column) of a field value in a JSON file\",\"datePublished\":\"2022-12-02T01:16:03+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\"},\"wordCount\":392,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"jq\",\"json\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\",\"name\":\"[Solved] Finding the location (line, column) of a field value in a JSON file - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-02T01:16:03+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Finding the location (line, column) of a field value in a JSON file\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Finding the location (line, column) of a field value in a JSON file - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Finding the location (line, column) of a field value in a JSON file - JassWeb","og_description":"[ad_1] Given JSON data and a query, there is no option in jq that, instead of printing the value itself, prints the location of possible matches. This is because JSON parsers providing an interface to developers usually focus on processing the logical structure of a JSON input, not the textual stream conveying it. You would ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/","og_site_name":"JassWeb","article_published_time":"2022-12-02T01:16:03+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Finding the location (line, column) of a field value in a JSON file","datePublished":"2022-12-02T01:16:03+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/"},"wordCount":392,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["jq","json"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/","url":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/","name":"[Solved] Finding the location (line, column) of a field value in a JSON file - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-02T01:16:03+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-finding-the-location-line-column-of-a-field-value-in-a-json-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Finding the location (line, column) of a field value in a JSON file"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=24342"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24342\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=24342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=24342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=24342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}