<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Convert collection of graphics in graphic layer to JSON format in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256859#M80217</link>
    <description>&lt;P&gt;This is great, additionally is there any way to apply this to MapNotesLayer considering its a collection of graphics layers?&lt;/P&gt;</description>
    <pubDate>Fri, 10 Feb 2023 03:44:50 GMT</pubDate>
    <dc:creator>Aeseir</dc:creator>
    <dc:date>2023-02-10T03:44:50Z</dc:date>
    <item>
      <title>Convert collection of graphics in graphic layer to JSON format</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256443#M80204</link>
      <description>&lt;P&gt;As per subject i have a graphics layer that contains set of graphics.&lt;/P&gt;&lt;P&gt;I want to be able to convert this collection of graphics to JSON format for local storage, with intent of recreating it later.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyone have advice with this?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 11:16:56 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256443#M80204</guid>
      <dc:creator>Aeseir</dc:creator>
      <dc:date>2023-02-09T11:16:56Z</dc:date>
    </item>
    <item>
      <title>Re: Convert collection of graphics in graphic layer to JSON format</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256458#M80205</link>
      <description>&lt;P&gt;I've been trying to do the very same thing and found that when using the stringify method on the graphics the type was being lost so I had to recreate a new object and save that out so that it would then be able to be drawn again later.&lt;/P&gt;&lt;P&gt;I could well be missing some simple OOB method, so maybe someone else will have a better answer but this works for me:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const result = [];

this.graphicsLayer.graphics.forEach((item) =&amp;gt; {
    // for some reason the stringify method on the graphic drops off the type of geometry (polygon, polyline etc.)
    // from the object so when we try to use the saved value to  draw it, it fails as the type isn't there.
    // so here we take the geometry of the item and them parse it into a new object, add the missing property
    // and pass that to the stringify method so that we have all of the expected properties at the other end.
    var geometry = JSON.parse(JSON.stringify(item.geometry));
    geometry.type = item.geometry.type;

    // add the newly constructed geometry to the result
    result.push(JSON.stringify(geometry));
});

return JSON.stringify(result);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This only applies to the geometry as that was all I was interested in keeping but similar should work if you need the other propeties.&lt;/P&gt;&lt;P&gt;Cheers&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 12:55:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256458#M80205</guid>
      <dc:creator>nevvo</dc:creator>
      <dc:date>2023-02-09T12:55:38Z</dc:date>
    </item>
    <item>
      <title>Re: Convert collection of graphics in graphic layer to JSON format</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256521#M80210</link>
      <description>&lt;P&gt;HI there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can use &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html#toJSON" target="_self"&gt;Graphic.toJSON()&lt;/A&gt; and &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-Graphic.html#fromJSON" target="_self"&gt;Graphic.fromJSON()&lt;/A&gt; methods to achieve what you are describing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can call &lt;A href="https://developers.arcgis.com/javascript/latest/api-reference/esri-views-layers-GraphicsLayerView.html#queryGraphics" target="_self"&gt;GraphicsLayerView.queryGraphics()&lt;/A&gt; method to get all graphics in the GraphicsLayer then get the json graphics.&amp;nbsp;&lt;/P&gt;&lt;P&gt;This simple codepen shows how to get an array of Graphic json objects by calling Graphic.toJSON() then how to add the json graphic objects to the GraphicsLayer by calling Graphic.fromJSON: &lt;A href="https://codepen.io/U_B_U/pen/rNrEyqg?editors=100" target="_blank"&gt;https://codepen.io/U_B_U/pen/rNrEyqg?editors=100&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Feb 2023 15:37:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256521#M80210</guid>
      <dc:creator>UndralBatsukh</dc:creator>
      <dc:date>2023-02-09T15:37:47Z</dc:date>
    </item>
    <item>
      <title>Re: Convert collection of graphics in graphic layer to JSON format</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256859#M80217</link>
      <description>&lt;P&gt;This is great, additionally is there any way to apply this to MapNotesLayer considering its a collection of graphics layers?&lt;/P&gt;</description>
      <pubDate>Fri, 10 Feb 2023 03:44:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/convert-collection-of-graphics-in-graphic-layer-to/m-p/1256859#M80217</guid>
      <dc:creator>Aeseir</dc:creator>
      <dc:date>2023-02-10T03:44:50Z</dc:date>
    </item>
  </channel>
</rss>

