<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en" xmlns="http://www.w3.org/2005/Atom"><title>Recent posts to JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/" rel="alternate"/><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/feed.atom" rel="self"/><id>https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/</id><updated>2025-10-09T08:51:49.324000Z</updated><subtitle>Recent posts to JSON (WString format) accessing elements. Convert to STRUCT?</subtitle><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#5236/8012" rel="alternate"/><published>2025-10-09T08:51:49.324000Z</published><updated>2025-10-09T08:51:49.324000Z</updated><author><name>Reinier Geers</name><uri>https://forge.codesys.com/u/reinier-geers/</uri></author><id>https://forge.codesys.com63a933e033dbb1ac68f77407e11930938c213104</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;HI ,  Ill try the same with Energy prices. But no data  I add the Query to the wsPost. Add $ because wstring use "as start end end.  Here the link of the API info&lt;br/&gt;
&lt;a href="https://reversed.notion.site/Marktprijzen-API-89ce600a88ac4abe8c2ad89d3167a83e" rel="nofollow"&gt;https://reversed.notion.site/Marktprijzen-API-89ce600a88ac4abe8c2ad89d3167a83e&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#5236/0d23" rel="alternate"/><published>2025-10-09T08:51:44.974000Z</published><updated>2025-10-09T08:51:44.974000Z</updated><author><name>Reinier Geers</name><uri>https://forge.codesys.com/u/reinier-geers/</uri></author><id>https://forge.codesys.comc96d03fc9988409b75f3e69847a4c782a23de0cd</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;HI ,  Ill try the same with Energy prices. But no data  I add the Query to the wsPost. Add $ because wstring use "as start end end.  Here the link of the API info&lt;br/&gt;
&lt;a href="https://reversed.notion.site/Marktprijzen-API-89ce600a88ac4abe8c2ad89d3167a83e" rel="nofollow"&gt;https://reversed.notion.site/Marktprijzen-API-89ce600a88ac4abe8c2ad89d3167a83e&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=50#524b/159c/ad02/55b1/e1af" rel="alternate"/><published>2023-05-23T12:30:04.906000Z</published><updated>2023-05-23T12:30:04.906000Z</updated><author><name>tvm</name><uri>https://forge.codesys.com/u/tvm/</uri></author><id>https://forge.codesys.comab7e77ac3c795b6bfb7fd69264ce023a0a1262fb</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;There's not actually any code in the JSONVAR function block itself, all the work is done in properties and methods.  If you post an example I can test it.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#524b/159c/ad02/55b1" rel="alternate"/><published>2023-05-21T08:13:16.262000Z</published><updated>2023-05-21T08:13:16.262000Z</updated><author><name>kdasystems</name><uri>https://forge.codesys.com/u/kdasystems/</uri></author><id>https://forge.codesys.com9eed400259f0c9cc0f963263eca80264391bff09</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Hello, can you share the code from the funcional block jsonvar. when I go to it is empty, only I can see the vars.&lt;br/&gt;
Actually I followed all conversation and at my site it doesnt work&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#524b/159c/ad02" rel="alternate"/><published>2023-03-31T01:49:04.052000Z</published><updated>2023-03-31T01:49:04.052000Z</updated><author><name>aott33</name><uri>https://forge.codesys.com/u/aott33/</uri></author><id>https://forge.codesys.com79606b41ad9b64b81f7757cf5a5781ba2b992e30</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks, TVM! I used the xReceived output from the Subscribe function block to trigger the ParsJSON function and it worked!&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#524b/159c" rel="alternate"/><published>2023-03-30T21:46:41.243000Z</published><updated>2023-03-30T21:46:41.243000Z</updated><author><name>aott33</name><uri>https://forge.codesys.com/u/aott33/</uri></author><id>https://forge.codesys.com35a1cb5e1db84df8050207423b31ae5f1de37082</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;That makes sense. I will do some testing and update you here. Thanks for pointing me in the right direction.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=50#524b" rel="alternate"/><published>2023-03-30T21:26:51.352000Z</published><updated>2023-03-30T21:26:51.352000Z</updated><author><name>tvm</name><uri>https://forge.codesys.com/u/tvm/</uri></author><id>https://forge.codesys.comf2d6ab4422a89e70137df499318c97241ef90258</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;I just tried your here and it works for me.  ParseJSON is looking for the rising edge of Execute, are you sure your sPayload variable has data in it before the parse is executed?&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#ed01/441e" rel="alternate"/><published>2023-03-30T02:24:53.590000Z</published><updated>2023-03-30T02:24:53.590000Z</updated><author><name>aott33</name><uri>https://forge.codesys.com/u/aott33/</uri></author><id>https://forge.codesys.com2d77518975c9f7b0a78a93b11b6e73359b8d7d56</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Thanks for creating this library. I have followed your directions and the parse json example is not working me. I found this thread as it is a similar application to mine. I am receiving a String Value from an MQTT subscribe function block. The issue is that I am getting null values.&lt;/p&gt;
&lt;p&gt;Below is my struct:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;TYPE FanRefSpeedJson :&lt;/span&gt;
&lt;span class="err"&gt;STRUCT&lt;/span&gt;
&lt;span class="err"&gt;    timestamp       : JSONVAR;&lt;/span&gt;
&lt;span class="err"&gt;    value           : JSONVAR;&lt;/span&gt;
&lt;span class="err"&gt;END_STRUCT&lt;/span&gt;
&lt;span class="err"&gt;END_TYPE&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Below are the relevant variables:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;VAR&lt;/span&gt;
&lt;span class="err"&gt;    sPayload                : STRING;&lt;/span&gt;
&lt;span class="err"&gt;    FanRefSpeedJSON_1       : FanRefSpeedJson;&lt;/span&gt;
&lt;span class="err"&gt;    ParseJSON               : JSON_TO_STRUCT;&lt;/span&gt;
&lt;span class="err"&gt;END_VAR&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Below is the return value from the MQTT Subscribe Function Block (sPayload)&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;'{"timestamp":1680099744985,"value":30}'&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Below is the ParseJSON code:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;ParseJSON(&lt;/span&gt;
&lt;span class="err"&gt;    Execute:= TRUE,&lt;/span&gt;
&lt;span class="err"&gt;    JSONString:= ADR(sPayload), &lt;/span&gt;
&lt;span class="err"&gt;    JSONStringSize:= SIZEOF(sPayload),&lt;/span&gt;
&lt;span class="err"&gt;    JSONVars:= ADR(FanRefSpeedJSON_1),&lt;/span&gt;
&lt;span class="err"&gt;    NumberOfVars:= SIZEOF(FanRefSpeedJSON_1) / SIZEOF(JSONVAR)&lt;/span&gt;
&lt;span class="err"&gt;);&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Attached are some screenshots showing the code and null values.&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=50#ed01" rel="alternate"/><published>2021-11-17T15:34:36.696000Z</published><updated>2021-11-17T15:34:36.696000Z</updated><author><name>tvm</name><uri>https://forge.codesys.com/u/tvm/</uri></author><id>https://forge.codesys.com82811bff45ba452d664ff65d034031e9bec0de56</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Here's an example of one of the structures I was using with Open Weather Map:&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="nt"&gt;TYPE&lt;/span&gt; &lt;span class="nt"&gt;OWM_WEATHER&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt;
&lt;span class="nt"&gt;STRUCT&lt;/span&gt;
    &lt;span class="nt"&gt;id&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;             &lt;span class="nt"&gt;JSONVAR&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;main&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;           &lt;span class="nt"&gt;JSONVAR&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;description&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="nt"&gt;JSONVAR&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="nt"&gt;icon&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;           &lt;span class="nt"&gt;JSONVAR&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="nt"&gt;END_STRUCT&lt;/span&gt;
&lt;span class="nt"&gt;END_TYPE&lt;/span&gt;

&lt;span class="nt"&gt;Weather&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;    &lt;span class="nt"&gt;OWM_WEATHER&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You'll have to create structures and arrays that match the expected result from whichever API you're using.  Order doesn't matter, but the structure does.  (But parsing will be faster if the order matches as closely as possible).&lt;/p&gt;
&lt;p&gt;So then, for example, if I want to get the description, I could access Weather.description.CharString&lt;br/&gt;
or if I wanted the id, I'd use Weather.id.Integer, (or Weather.id.Number if I wanted it as a floating point value)&lt;/p&gt;
&lt;p&gt;take a look at the documentation in the JSONVAR function block, basically it guesses at the type when parsing from the string (because it uses the setter of the .AsString property), but you can then access it as whatever type you need (string, boolean, integer, number, or datetime)&lt;/p&gt;&lt;/div&gt;</summary></entry><entry><title>JSON (WString format) accessing elements. Convert to STRUCT?</title><link href="https://forge.codesys.com/forge/talk/Engineering/thread/67f3c76128/?limit=25#6351" rel="alternate"/><published>2021-11-17T14:45:17.144000Z</published><updated>2021-11-17T14:45:17.144000Z</updated><author><name>justthefacts77</name><uri>https://forge.codesys.com/u/justthefacts77/</uri></author><id>https://forge.codesys.comc4eb7e4925fac9f882caf671a907cd8419da6601</id><summary type="html">&lt;div class="markdown_content"&gt;&lt;p&gt;Ok so looking at "ParseJSON"...feed it in a String, then how do I access members of an Element, then an object in an Array, i.e. how does it determine which Type?&lt;/p&gt;
&lt;p&gt;"coord" : {&lt;/p&gt;
&lt;div class="codehilite"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class="err"&gt;"lon" : -82.4584,&lt;/span&gt;
&lt;span class="err"&gt;"lat" : 27.9475&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;},&lt;br/&gt;
"weather" : [&lt;/p&gt;
&lt;p&gt;{&lt;br/&gt;
        "id" : 804,&lt;br/&gt;
        "main" : Clouds,&lt;br/&gt;
        "description" : overcast clouds,&lt;br/&gt;
        "icon" : 04d&lt;br/&gt;
    }&lt;/p&gt;
&lt;p&gt;],&lt;/p&gt;
&lt;p&gt;?- What would be the syntax to assign the element to a temp variable, for example&lt;br/&gt;
temp := JSONVars.weather.main&lt;/p&gt;
&lt;p&gt;Appreciate any input, gonna have to do my due diligence on the coding aspects for sure.&lt;/p&gt;&lt;/div&gt;</summary></entry></feed>