This commit is contained in:
nlohmann committed 2026-10-27 12:14:00 +00:00
1 parent b36f8a9821
commit 11a863958e
1180 files changed
+14211 -6025

No files matched your search

+3 -3
View File
@@ -1,4 +1,4 @@
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/json_pointer/" rel="canonical"><link href=../json_patch/ rel=prev><link href=../modifying_values/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>JSON Pointer - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel="stylesheet" href="../../assets/external/fonts.googleapis.com/css.61a430c9.css"><style>:root{--md-text-font:"Roboto";--md-code-font:"JetBrains Mono"}</style><link rel=stylesheet href=../../css/custom.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#json-pointer class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a3 3 0 0 0 3-3 3 3 0 0 0-3-3 3 3 0 0 0-3 3 3 3 0 0 0 3 3m0 3.54C9.64 9.35 6.5 8 3 8v11c3.5 0 6.64 1.35 9 3.54 2.36-2.19 5.5-3.54 9-3.54V8c-3.5 0-6.64 1.35-9 3.54"/></svg> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> JSON Pointer </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=indigo data-md-color-accent=indigo aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.52 6.52 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5"/></svg> </label> <div claLine truncated
<!doctype html><html lang=en class=no-js> <head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=description content="JSON for Modern C++ is a header-only C++11 library that makes JSON a first-class data type."><meta name=author content="Niels Lohmann"><link href="https://json.nlohmann.me/features/json_pointer/" rel="canonical"><link href=../json_patch/ rel=prev><link href=../modifying_values/ rel=next><link rel=icon href=../../assets/images/favicon.png><meta name=generator content="mkdocs-1.6.1, mkdocs-material-9.7.7"><title>JSON Pointer - JSON for Modern C++</title><link rel=stylesheet href=../../assets/stylesheets/main.ec1eaa64.min.css><link rel=stylesheet href=../../assets/stylesheets/palette.ab4e12ef.min.css><link rel=stylesheet href=../../css/custom.css><link rel=stylesheet href=../../css/brand.css><link rel=stylesheet href=../../css/fig-sax.css><link rel=stylesheet href=../../css/fig-jsonview.css><link rel=stylesheet href=../../css/fig-exceptions.css><link rel=stylesheet href=../../css/fig-pointer.css><link rel=stylesheet href=../../css/fig-charts.css><link rel=stylesheet href=../../css/fig-integration.css><link rel=stylesheet href=../../css/fig-macros.css><script>__md_scope=new URL("../..",location),__md_hash=e=>[...e].reduce(((e,_)=>(e<<5)-e+_.charCodeAt(0)),0),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script></head> <body dir=ltr data-md-color-scheme=default data-md-color-primary=custom data-md-color-accent=custom> <input class=md-toggle data-md-toggle=drawer type=checkbox id=__drawer autocomplete=off> <input class=md-toggle data-md-toggle=search type=checkbox id=__search autocomplete=off> <label class=md-overlay for=__drawer></label> <div data-md-component=skip> <a href=#json-pointer class=md-skip> Skip to content </a> </div> <div data-md-component=announce> </div> <header class=md-header data-md-component=header> <nav class="md-header__inner md-grid" aria-label=Header> <a href=../.. title="JSON for Modern C++" class="md-header__button md-logo" aria-label="JSON for Modern C++" data-md-component=logo> <img src=../../images/wordmark-header.svg alt=logo> </a> <label class="md-header__button md-icon" for=__drawer> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M3 6h18v2H3zm0 5h18v2H3zm0 5h18v2H3z"/></svg> </label> <div class=md-header__title data-md-component=header-title> <div class=md-header__ellipsis> <div class=md-header__topic> <span class=md-ellipsis> JSON for Modern C++ </span> </div> <div class=md-header__topic data-md-component=header-topic> <span class=md-ellipsis> JSON Pointer </span> </div> </div> </div> <form class=md-header__option data-md-component=palette> <input class=md-option data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme=default data-md-color-primary=custom data-md-color-accent=custom aria-label="Switch to dark mode" type=radio name=__palette id=__palette_0> <label class="md-header__button md-icon" title="Switch to dark mode" for=__palette_1 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 8a4 4 0 0 0-4 4 4 4 0 0 0 4 4 4 4 0 0 0 4-4 4 4 0 0 0-4-4m0 10a6 6 0 0 1-6-6 6 6 0 0 1 6-6 6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> <input class=md-option data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme=slate data-md-color-primary=custom data-md-color-accent=custom aria-label="Switch to light mode" type=radio name=__palette id=__palette_1> <label class="md-header__button md-icon" title="Switch to light mode" for=__palette_0 hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M12 18c-.89 0-1.74-.2-2.5-.55C11.56 16.5 13 14.42 13 12s-1.44-4.5-3.5-5.45C10.26 6.2 11.11 6 12 6a6 6 0 0 1 6 6 6 6 0 0 1-6 6m8-9.31V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12z"/></svg> </label> </form> <script>var palette=__md_get("__palette");if(palette&&palette.color){if("(prefers-color-scheme)"===palette.color.media){var media=matchMedia("(prefers-color-scheme: light)"),input=document.querySelector(media.matches?"[data-md-color-media='(prefers-color-scheme: light)']":"[data-md-color-media='(prefers-color-scheme: dark)']");palette.color.media=input.getAttribute("data-md-color-media"),palette.color.scheme=input.getAttribute("data-md-color-scheme"),palette.color.primary=input.getAttribute("data-md-color-primary"),palette.color.accent=input.getAttribute("data-md-color-accent")}for(var[key,value]of Object.entries(palette.color))document.body.setAttribute("data-md-color-"+key,value)}</script> <label class="md-header__button md-icon" for=__search> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.2Line truncated
<span class=w> </span><span class=nt>"array"</span><span class=p>:</span><span class=w> </span><span class=p>[</span><span class=s2>"A"</span><span class=p>,</span><span class=w> </span><span class=s2>"B"</span><span class=p>,</span><span class=w> </span><span class=s2>"C"</span><span class=p>],</span>
<span class=w> </span><span class=nt>"nested"</span><span class=p>:</span><span class=w> </span><span class=p>{</span>
<span class=w> </span><span class=nt>"one"</span><span class=p>:</span><span class=w> </span><span class=mi>1</span><span class=p>,</span>
@@ -6,7 +6,7 @@
<span class=w> </span><span class=nt>"three"</span><span class=p>:</span><span class=w> </span><span class=p>[</span><span class=kc>true</span><span class=p>,</span><span class=w> </span><span class=kc>false</span><span class=p>]</span>
<span class=w> </span><span class=p>}</span>
<span class=p>}</span>
</code></pre></div> <p>Then every value inside the <abbr title="JavaScript Object Notation">JSON</abbr> document can be identified as follows:</p> <table> <thead> <tr> <th><abbr title="JavaScript Object Notation">JSON</abbr> Pointer</th> <th><abbr title="JavaScript Object Notation">JSON</abbr> value</th> </tr> </thead> <tbody> <tr> <td>``</td> <td><code class=highlight><span class=p>{</span><span class=nt>"array"</span><span class=p>:[</span><span class=s2>"A"</span><span class=p>,</span><span class=s2>"B"</span><span class=p>,</span><span class=s2>"C"</span><span class=p>],</span><span class=nt>"nested"</span><span class=p>:{</span><span class=nt>"one"</span><span class=p>:</span><span class=mi>1</span><span class=p>,</span><span class=nt>"two"</span><span class=p>:</span><span class=mi>2</span><span class=p>,</span><span class=nt>"three"</span><span class=p>:[</span><span class=kc>true</span><span class=p>,</span><span class=kc>false</span><span class=p>]}}</span></code></td> </tr> <tr> <td><code>/array</code></td> <td><code class=highlight><span class=p>[</span><span class=s2>"A"</span><span class=p>,</span><span class=s2>"B"</span><span class=p>,</span><span class=s2>"C"</span><span class=p>]</span></code></td> </tr> <tr> <td><code>/array/0</code></td> <td><code class=highlight><span class=err>A</span></code></td> </tr> <tr> <td><code>/array/1</code></td> <td><code class=highlight><span class=err>B</span></code></td> </tr> <tr> <td><code>/array/2</code></td> <td><code class=highlight><span class=err>C</span></code></td> </tr> <tr> <td><code>/nested</code></td> <td><code class=highlight><span class=p>{</span><span class=nt>"one"</span><span class=p>:</span><span class=mi>1</span><span class=p>,</span><span class=nt>"two"</span><span class=p>:</span><span class=mi>2</span><span class=p>,</span><span class=nt>"three"</span><span class=p>:[</span><span class=kc>true</span><span class=p>,</span><span class=kc>false</span><span class=p>]}</span></code></td> </tr> <tr> <td><code>/nested/one</code></td> <td><code class=highlight><span class=mi>1</span></code></td> </tr> <tr> <td><code>/nested/two</code></td> <td><code class=highlight><span class=mi>2</span></code></td> </tr> <tr> <td><code>/nested/three</code></td> <td><code class=highlight><span class=p>[</span><span class=kc>true</span><span class=p>,</span><span class=kc>false</span><span class=p>]</span></code></td> </tr> <tr> <td><code>/nested/three/0</code></td> <td><code class=highlight><span class=kc>true</span></code></td> </tr> <tr> <td><code>/nested/three/1</code></td> <td><code class=highlight><span class=kc>false</span></code></td> </tr> </tbody> </table> <p>Note <code>/</code> does not identify the root (i.e., the whole document), but an object entry with empty key <code>""</code>. See <a href="https://tools.ietf.org/html/rfc6901"><abbr title="Request for Comments">RFC</abbr> 6901</a> for more information.</p> <p><a href="https://tools.ietf.org/html/rfc6901#section-4"><abbr title="Request for Comments">RFC</abbr> 6901, section 4</a> states that evaluation fails if an object has non-unique member names. A <code>basic_json</code> object holds one value per key (see <a href=../../api/basic_json/object_t/ ><code>object_t</code></a> for how duplicate keys are handled while parsing), so evaluating a <abbr title="JavaScript Object Notation">JSON</abbr> Pointer never encounters duplicates.</p> <h2 id=json-pointer-creation><abbr title="JavaScript Object Notation">JSON</abbr> Pointer creation<a class=headerlink href=#json-pointer-creation title="Permanent link">&para;</a></h2> <p><abbr title="JavaScript Object Notation">JSON</abbr> Pointers can be created from a string:</p> <div class=highlight><pre><span></span><code><span class=n>json</span><span class=o>::</span><span class=n>json_pointer</span><span class=w> </span><span class=n>p</span><span class=p>(</span><span class=s>"/nested/one"</span><span class=p>);</span>
</code></pre></div> <p>Select a pointer (or a node of the tree) to walk it through this document; each segment of the pointer goes one level deeper. The table below lists the same pointers.</p> <div class=nj-pointer-walk data-nj-pointer-walk data-doc='{"array":["A","B","C"],"nested":{"one":1,"two":2,"three":[true,false]}}' role=group aria-label="Pointer walker for the example document"> <p class=nj-pointer-static>Highlighted: the path of the pointer <code>/nested/three/1</code>, which identifies <code>false</code>.</p> <ul class=nj-pointer-tree> <li data-p class=is-on><span class=nj-pointer-n><span class=nj-pointer-k>root</span> <span class=nj-pointer-t>object</span></span> <ul> <li data-p=/array class=is-pass><span class=nj-pointer-n><span class=nj-pointer-k>&quot;array&quot;</span> <span class=nj-pointer-t>array</span></span> <ul> <li data-p=/array/0><span class=nj-pointer-n><span class=nj-pointer-k>0</span>: <span class="nj-pointer-v nj-pointer-v--s">&quot;A&quot;</span></span></li> <li data-p=/array/1><span class=nj-pointer-n><span class=nj-pointer-k>1</span>: <span class="nj-pointer-v nj-pointer-v--s">&quot;B&quot;</span></span></li> <li data-p=/array/2><span class=nj-pointer-n><span class=nj-pointer-k>2</span>: <span class="nj-pointer-v nj-pointer-v--s">&quot;C&quot;</span></span></li> </ul> </li> <li data-p=/nested class=is-on><span class=nj-pointer-n><span class=nj-pointer-k>&quot;nested&quot;</span> <span class=nj-pointer-t>object</span></span> <ul> <li data-p=/nested/one class=is-pass><span class=nj-pointer-n><span class=nj-pointer-k>&quot;one&quot;</span>: <span class="nj-pointer-v nj-pointer-v--n">1</span></span></li> <li data-p=/nested/two class=is-pass><span class=nj-pointer-n><span class=nj-pointer-k>&quot;two&quot;</span>: <span class="nj-pointer-v nj-pointer-v--n">2</span></span></li> <li data-p=/nested/three class=is-on><span class=nj-pointer-n><span class=nj-pointer-k>&quot;three&quot;</span> <span class=nj-pointer-t>array</span></span> <ul> <li data-p=/nested/three/0 class=is-pass><span class=nj-pointer-n><span class=nj-pointer-k>0</span>: <span class="nj-pointer-v nj-pointer-v--b">true</span></span></li> <li data-p=/nested/three/1 class="is-on is-target"><span class=nj-pointer-n><span class=nj-pointer-k>1</span>: <span class="nj-pointer-v nj-pointer-v--b">false</span></span></li> </ul> </li> </ul> </li> </ul> </li> </ul> </div> <p class=nj-caption>The highlighted nodes are the path from the root to the selected level, and the line below the tree shows the value the pointer resolves to. Select a segment in the pointer bar to stop at a shallower level, or a node of the tree to select its pointer.</p> <p>Then every value inside the <abbr title="JavaScript Object Notation">JSON</abbr> document can be identified as follows:</p> <table> <thead> <tr> <th><abbr title="JavaScript Object Notation">JSON</abbr> Pointer</th> <th><abbr title="JavaScript Object Notation">JSON</abbr> value</th> </tr> </thead> <tbody> <tr> <td>``</td> <td><code class=highlight><span class=p>{</span><span class=nt>"array"</span><span class=p>:[</span><span class=s2>"A"</span><span class=p>,</span><span class=s2>"B"</span><span class=p>,</span><span class=s2>"C"</span><span class=p>],</span><span class=nt>"nested"</span><span class=p>:{</span><span class=nt>"one"</span><span class=p>:</span><span class=mi>1</span><span class=p>,</span><span class=nt>"two"</span><span class=p>:</span><span class=mi>2</span><span class=p>,</span><span class=nt>"three"</span><span class=p>:[</span><span class=kc>true</span><span class=p>,</span><span class=kc>false</span><span class=p>]}}</span></code></td> </tr> <tr> <td><code>/array</code></td> <td><code class=highlight><span class=p>[</span><span class=s2>"A"</span><span class=p>,</span><span class=s2>"B"</span><span class=p>,</span><span class=s2>"C"</span><span class=p>]</span></code></td> </tr> <tr> <td><code>/array/0</code></td> <td><code class=highlight><span class=err>A</span></code></td> </tr> <tr> <td><code>/array/1</code></td> <td><code class=highlight><span class=err>B</span></code></td> </tr> <tr> <td><code>/array/2</code></td> <td><code class=highlight><span class=err>C</span></code></td> </tr> <tr> <td><code>/nested</code></td> <td><code class=highlight><span class=p>{</span><span class=nt>"one"</span><span class=p>:</span><span class=mi>1</span><span class=p>,</span><span class=nt>"two"</span><span class=p>:</span><span class=mi>2</span><span class=p>,</span><span class=nt>"three"</span><span class=p>:[</span><span class=kc>true</span><span class=p>,</span><span class=kc>false</span><span class=p>]}</span></code></td> </tr> <tr> <td><code>/nested/one</code></td> <td><code class=highlight><span class=mi>1</span></code></td> </tr> <tr> <td><code>/nested/two</code></td> <td><code class=highlight><span class=mi>2</span></code></td> </tr> <tr> <td><code>/nested/three</code></td> <td><code class=highlight><span class=p>[</span><span class=kc>true</span><span class=p>,</span><spanLine truncated
</code></pre></div> <p>Furthermore, a user-defined string literal can be used to achieve the same result:</p> <div class=highlight><pre><span></span><code><span class=k>auto</span><span class=w> </span><span class=n>p</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=s>"/nested/one"</span><span class=n>_json_pointer</span><span class=p>;</span>
</code></pre></div> <p>The escaping rules of <a href="https://tools.ietf.org/html/rfc6901"><abbr title="Request for Comments">RFC</abbr> 6901</a> are implemented. See the <a href=../../api/json_pointer/json_pointer/ >constructor documentation</a> for more information.</p> <div class="admonition note"> <p class=admonition-title>URI fragment representation</p> <p><a href="https://tools.ietf.org/html/rfc6901#section-6"><abbr title="Request for Comments">RFC</abbr> 6901, section 6</a> also defines a URI fragment identifier representation (<code>#/a%20b</code>). It is not supported: <code class=highlight><span class=n>json</span><span class=o>::</span><span class=n>json_pointer</span><span class=p>(</span><span class=s>"#/a"</span><span class=p>)</span></code> throws <a href=../../home/exceptions/#jsonexceptionparse_error107><code>parse_error.107</code></a>. Strip the leading <code>#</code> and percent-decode the remainder before creating the <abbr title="JavaScript Object Notation">JSON</abbr> pointer.</p> </div> <h2 id=value-access>Value access<a class=headerlink href=#value-access title="Permanent link">&para;</a></h2> <p><abbr title="JavaScript Object Notation">JSON</abbr> Pointers can be used in the <a href=../../api/basic_json/at/ ><code>at</code></a>, <a href=../../api/basic_json/operator%5B%5D/ ><code>operator[]</code></a>, and <a href=../../api/basic_json/value/ ><code>value</code></a> functions just like object keys or array indices.</p> <div class=highlight><pre><span></span><code><span class=c1>// the JSON value from above</span>
<span class=k>auto</span><span class=w> </span><span class=n>j</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>json</span><span class=o>::</span><span class=n>parse</span><span class=p>(</span><span class=sa>R</span><span class=s>"</span><span class=dl>(</span><span class=s>{</span>
@@ -45,4 +45,4 @@
<span class=w> </span><span class=nt>"/nested/three/1"</span><span class=p>:</span><span class=w> </span><span class=kc>false</span>
<span class=p>}</span>
</code></pre></div> <p>The reverse function, <a href=../../api/basic_json/unflatten/ ><code>unflatten</code></a> recreates the original value.</p> <div class=highlight><pre><span></span><code><span class=k>auto</span><span class=w> </span><span class=n>j_original</span><span class=w> </span><span class=o>=</span><span class=w> </span><span class=n>j_flat</span><span class=p>.</span><span class=n>unflatten</span><span class=p>();</span>
</code></pre></div> <h2 id=see-also>See also<a class=headerlink href=#see-also title="Permanent link">&para;</a></h2> <ul> <li>Class <a href=../../api/json_pointer/ ><code>json_pointer</code></a></li> <li>Functions <a href=../../api/basic_json/value/ ><code>value</code></a>, <a href=../../api/basic_json/contains/ ><code>contains</code></a>, and <a href=../../api/basic_json/at/ ><code>at</code></a> accept <abbr title="JavaScript Object Notation">JSON</abbr> Pointers; see <a href=../element_access/default_value/#nested-values>Nested values</a></li> <li>Function <a href=../../api/basic_json/flatten/ ><code>flatten</code></a></li> <li>Function <a href=../../api/basic_json/unflatten/ ><code>unflatten</code></a></li> <li><a href=../json_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Patch</a> - paths inside a patch are <abbr title="JavaScript Object Notation">JSON</abbr> Pointers</li> <li><a href=../merge_patch/ ><abbr title="JavaScript Object Notation">JSON</abbr> Merge Patch</a> - an alternative patch format that does not use <abbr title="JavaScript Object Notation">JSON</abbr> Pointer</li> </ul> <!-- https://squidfunk.github.io/mkdocs-material/reference/tooltips/#adding-a-glossary --> <aside class=md-source-file> <span class=md-source-file__fact> <span class=md-icon title="Last update"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M21 13.1c-.1 0-.3.1-.4.2l-1 1 2.1 2.1 1-1c.2-.2.2-.6 0-.8l-1.3-1.3c-.1-.1-.2-.2-.4-.2m-1.9 1.8-6.1 6V23h2.1l6.1-6.1zM12.5 7v5.2l4 2.4-1 1L11 13V7zM11 21.9c-5.1-.5-9-4.8-9-9.9C2 6.5 6.5 2 12 2c5.3 0 9.6 4.1 10 9.3-.3-.1-.6-.2-1-.2s-.7.1-1 .2C19.6 7.2 16.2 4 12 4c-4.4 0-8 3.6-8 8 0 4.1 3.1 7.5 7.1 7.9l-.1.2z"/></svg> </span> <span class="git-revision-date-localized-plugin git-revision-date-localized-plugin-date" title="October 26, 2026 16:11:46 UTC">October 26, 2026</span> </span> </aside> </article> </div> <script>var tabs=__md_get("__tabs");if(Array.isArray(tabs))e:for(var set of document.querySelectorAll(".tabbed-set")){var labels=set.querySelector(".tabbed-labels");for(var tab of tabs)for(var label of labels.getElementsByTagName("label"))if(label.innerText.trim()===tab){var input=document.getElementById(label.htmlFor);input.checked=!0;continue e}}</script> <script>var target=document.getElementById(location.hash.slice(1));target&&target.name&&(target.checked=target.name.startsWith("__tabbed_"))</script> </div> <button type=button class="md-top md-icon" data-md-component=top hidden> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 24 24"><path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8z"/></svg> Back to top </button> </main> <footer class=md-footer> <div class="md-footer-meta md-typeset"> <div class="md-footer-meta__inner md-grid"> <div class=md-copyright> <div class=md-copyright__highlight> Copyright &copy; 2013-2026 Niels Lohmann </div> </div> <div class=md-social> <a href="https://github.com/nlohmann" target="_blank" rel="noopener" title="github.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 512 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.--><path d="M173.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6m-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3m44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9M252.8 8C114.1 8 8 113.3 8 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C436.2 457.8 504 362.9 504 252 504 113.3 391.5 8 252.8 8M105.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1m-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7m32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1m-11.4-14.7c-1.6 1-1.6 3.6 0 5.9s4.3 3.3 5.6 2.3c1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2"/></svg> </a> <a href="https://www.linkedin.com/in/nielslohmann/" target="_blank" rel="noopener" title="www.linkedin.com" class="md-social__link"> <svg xmlns=http://www.w3.org/2000/svg viewbox="0 0 448 512"><!-- Font Awesome Free 7.1.0 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License) Copyright 2025 Fonticons, Inc.Line truncated
</code></pre></div> <p>Each leaf of the document becomes one key of the flat object, and <code>unflatten()</code> reverses the mapping:</p> <div class=nj-fig-wrap> <p><svg class="nj-fig nj-pointer-fl" viewbox="0 0 760 442" role=img aria-labelledby=fig-flat-title xmlns=http://www.w3.org/2000/svg> <title id=fig-flat-title>flatten() turns the nested example document into a flat object, and unflatten() turns it back. Left, the document as a tree: array with the elements A, B, C, and nested with one = 1, two = 2, and three with the elements true and false. Right, the flat object with one key per leaf: /array/0 = A, /array/1 = B, /array/2 = C, /nested/one = 1, /nested/two = 2, /nested/three/0 = true, /nested/three/1 = false. Each leaf is joined to its key by a line.</title> <defs><marker id=fig-flat-arrow viewbox="0 0 10 10" refx=9 refy=5 markerwidth=7 markerheight=7 orient=auto><path class=fg-head d="M0 1 L10 5 L0 9 z"/></marker></defs> <text class=fg-h x=20 y=26>nested value</text> <text class=fg-h x=470 y=26>flat object: key = J&#8203;SON Pointer</text> <line class=fg-arrow x1=250 y1=52 x2=450 y2=52 marker-end=url(#fig-flat-arrow) /> <text class=nj-pointer-fl-op x=350 y=44>flatten()</text> <line class=fg-arrow x1=450 y1=420 x2=250 y2=420 marker-end=url(#fig-flat-arrow) /> <text class=nj-pointer-fl-op x=350 y=412>unflatten()</text> <path class=nj-pointer-fl-edge d="M25 99 V115 H42"/> <path class=nj-pointer-fl-edge d="M51 127 V143 H68"/> <path class=nj-pointer-fl-edge d="M51 127 V171 H68"/> <path class=nj-pointer-fl-edge d="M51 127 V199 H68"/> <path class=nj-pointer-fl-edge d="M25 99 V227 H42"/> <path class=nj-pointer-fl-edge d="M51 239 V255 H68"/> <path class=nj-pointer-fl-edge d="M51 239 V283 H68"/> <path class=nj-pointer-fl-edge d="M51 239 V311 H68"/> <path class=nj-pointer-fl-edge d="M77 323 V339 H94"/> <path class=nj-pointer-fl-edge d="M77 323 V367 H94"/> <path class="nj-pointer-fl-link nj-pointer-fl--s" d="M139.6 143 C259.6 143 340 171 460 171"/> <circle cx=139.6 cy=143 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--s"/> <text class=fg-code x=470 y=176 xml:space=preserve><tspan class=nj-pointer-fl-key>"/array/0"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--s">"A"</tspan></text> <path class="nj-pointer-fl-link nj-pointer-fl--s" d="M139.6 171 C259.6 171 340 199 460 199"/> <circle cx=139.6 cy=171 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--s"/> <text class=fg-code x=470 y=204 xml:space=preserve><tspan class=nj-pointer-fl-key>"/array/1"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--s">"B"</tspan></text> <path class="nj-pointer-fl-link nj-pointer-fl--s" d="M139.6 199 C259.6 199 340 227 460 227"/> <circle cx=139.6 cy=199 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--s"/> <text class=fg-code x=470 y=232 xml:space=preserve><tspan class=nj-pointer-fl-key>"/array/2"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--s">"C"</tspan></text> <path class="nj-pointer-fl-link nj-pointer-fl--n" d="M158.8 255 C278.8 255 340 255 460 255"/> <circle cx=158.8 cy=255 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--n"/> <text class=fg-code x=470 y=260 xml:space=preserve><tspan class=nj-pointer-fl-key>"/nested/one"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--n">1</tspan></text> <path class="nj-pointer-fl-link nj-pointer-fl--n" d="M158.8 283 C278.8 283 340 283 460 283"/> <circle cx=158.8 cy=283 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--n"/> <text class=fg-code x=470 y=288 xml:space=preserve><tspan class=nj-pointer-fl-key>"/nested/two"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--n">2</tspan></text> <path class="nj-pointer-fl-link nj-pointer-fl--b" d="M175.2 339 C295.2 339 340 311 460 311"/> <circle cx=175.2 cy=339 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--b"/> <text class=fg-code x=470 y=316 xml:space=preserve><tspan class=nj-pointer-fl-key>"/nested/three/0"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--b">true</tspan></text> <path class="nj-pointer-fl-link nj-pointer-fl--b" d="M184.8 367 C304.8 367 340 339 460 339"/> <circle cx=184.8 cy=367 r=3.5 class="nj-pointer-fl-dot nj-pointer-fl--b"/> <text class=fg-code x=470 y=344 xml:space=preserve><tspan class=nj-pointer-fl-key>"/nested/three/1"</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--b">false</tspan></text> <text class=fg-h x=20 y=92>root</text> <text class="fg-code nj-pointer-fl-inner" x=46 y=120>"array"</text> <text class=fg-code x=72 y=148 xml:space=preserve><tspan class=nj-pointer-fl-k>0</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--s">"A"</tspan></text> <text class=fg-code x=72 y=176 xml:space=preserve><tspan class=nj-pointer-fl-k>1</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--s">"B"</tspan></text> <text class=fg-code x=72 y=204 xml:space=preserve><tspan class=nj-pointer-fl-k>2</tspan>: <tspan class="nj-pointer-fl-v nj-pointer-fl--s">"C"</tspan></text> <text class="fg-code nj-pointer-fl-inner" x=46 y=232>"nested"</text> <text class=fg-code x=72 y=260 xml:space=preserve><tspan class=nj-pointer-fl-k>Line truncated
+28 -4
View File
@@ -17,6 +17,24 @@ Consider the following JSON document
}
```
Select a pointer (or a node of the tree) to walk it through this document; each segment of the pointer goes one level deeper. The table below lists the same pointers.
Highlighted: the path of the pointer `/nested/three/1`, which identifies `false`.
- root object
- "array" array
- 0: "A"
- 1: "B"
- 2: "C"
- "nested" object
- "one": 1
- "two": 2
- "three" array
- 0: true
- 1: false
The highlighted nodes are the path from the root to the selected level, and the line below the tree shows the value the pointer resolves to. Select a segment in the pointer bar to stop at a shallower level, or a node of the tree to select its pointer.
Then every value inside the JSON document can be identified as follows:
| JSON Pointer | JSON value |
@@ -124,11 +142,17 @@ The reverse function, [`unflatten`](https://json.nlohmann.me/api/basic_json/unfl
auto j_original = j_flat.unflatten();
```
Each leaf of the document becomes one key of the flat object, and `unflatten()` reverses the mapping:
Left: the example document as a tree; right: the flat object it becomes. `flatten()` converts from left to right, `unflatten()` from right to left.
## See also
- Class [`json_pointer`](https://json.nlohmann.me/api/json_pointer/index.md)
- Functions [`value`](https://json.nlohmann.me/api/basic_json/value/index.md), [`contains`](https://json.nlohmann.me/api/basic_json/contains/index.md), and [`at`](https://json.nlohmann.me/api/basic_json/at/index.md) accept JSON Pointers; see [Nested values](https://json.nlohmann.me/features/element_access/default_value/#nested-values)
- Function [`flatten`](https://json.nlohmann.me/api/basic_json/flatten/index.md)
- Function [`unflatten`](https://json.nlohmann.me/api/basic_json/unflatten/index.md)
- [`json_pointer`](https://json.nlohmann.me/api/json_pointer/index.md) - the JSON Pointer class
- [Nested values](https://json.nlohmann.me/features/element_access/default_value/#nested-values) - [`value`](https://json.nlohmann.me/api/basic_json/value/index.md), [`contains`](https://json.nlohmann.me/api/basic_json/contains/index.md), and [`at`](https://json.nlohmann.me/api/basic_json/at/index.md) accept JSON Pointers
- [`flatten`](https://json.nlohmann.me/api/basic_json/flatten/index.md) - convert a value to an object of JSON Pointers and primitive values
- [`unflatten`](https://json.nlohmann.me/api/basic_json/unflatten/index.md) - restore a value from a flattened object
- [JSON Patch](https://json.nlohmann.me/features/json_patch/index.md) - paths inside a patch are JSON Pointers
- [JSON Merge Patch](https://json.nlohmann.me/features/merge_patch/index.md) - an alternative patch format that does not use JSON Pointer
- [`operator""_json_pointer`](https://json.nlohmann.me/api/operator_literal_json_pointer/index.md) - user-defined literal to create a JSON pointer
- [Element Access](https://json.nlohmann.me/features/element_access/index.md) - other ways to access elements