E!cockpit JSON string, sort data

Hello. I’m trying and retrieve information from a JSON file. I manage to pick up the json string. But I can’t sort the data with WagoAppJSON.FbWrite_ToIEC_ByRule.
The data I try and retrieve is electricity prices in Norway during 24 hours.

link to json file https://www.hvakosterstrommen.no/api/v1/prices/2022/10-30_NO5.json

VAR
	FbWrite_ToIEC_ByRuleStrom		: WagoAppJSON.FbWrite_ToIEC_ByRule;		// Function block to sort the info from the JSON file
	stringPath						: STRING(20):='/';						// Path the JSON file uses
	My1								: FbWrite_ToIEC_ByRule;
 	xHttpsGetTrigger2				: BOOL;
	xWriteToIEC_Error				: BOOL;
	iWriteToIEC_ActualArrayRow		: INT;

	MyJsonData		: ARRAY[0..23] OF typEnergyPrice;						// JSON data to sort the data inn
	
	bTestRule		: ARRAY[0..MAX_ARRAY_ELEMENTS_CUSTOM] OF typRuleBase:=[								
							(sPath:='NOK_per_kWh',iConvertType:=eReal),		// 1
							(sPath:='EUR_per_kWh',iConvertType:=eReal),		// 2
							(sPath:='EXR',iConvertType:=eReal),				// 3
							(sPath:='time_start',iConvertType:=eString),	// 4	Tride to use eTime but got error
							(sPath:='time_end',iConvertType:=eString)		// 5
						];
END_VAR 

FbWrite_ToIEC_ByRuleStrom(														// Function block to sort the info from the JSON file
	sPathBase:= stringPath, 
	pDataIn:= ADR(unHttpsGetRxBuffer.sVar), 
	udiSizeDataIn:= SIZEOF(unHttpsGetRxBuffer.abVar), 
	aRule:= bTestRule, 
	pIecVariable:= ADR(MyJsonData), 
	iArraySizeIecVariable:= 24, 
	udiSizeIecVariable:= SIZEOF(MyJsonData), 
	xError=> xWriteToIEC_Error, 
	iActualArrayRow=> iWriteToIEC_ActualArrayRow, 
	xTrigger:= xHttpsGetTrigger2 );												// xHttpsGetTrigger2 = TRUE to run

I made a GitLab project with all the info inn.
https://gitlab.com/espenbo/Codesys_JSON_hvakosterstrommen/-/tree/main

I’ll be happy if someone can help. Mabey I could make a HowTo for other that are trying the same.

Hi @espenbo ,
For this purpose, the SAX parser of the WagoAppJSON is perfect.
You just need to define the token, and the associated rule to process the string data.

You’ll find attached the way to implement it in your example.
eCockpit_Json.txt (139.7 KB)
(This is an export file change the extension file to .export)
Set xTriggerJSONParser to start the parsing process.

3 Likes

Thanks.
I got it to work. Get my prices. I added a sort function. And fixed a bug when the days a between 1 and 9.

I Uploaded a new version on GitLab if someone else is interested.

1 Like

I see that you created some big function for formatting time, take a look at WagoAppString.TimeComponents_To_String where you can easy format time/date:

2 Likes

I am continuing to work on this project and have added HMI tables. I also added extra information in typEnergyPrice.
There I added iArrayPlace to have an overview of where in the array the info is located when I sort from cheap to most expensive. But when I added iArrayPlace then I get weird values in HMI tables


In array 1, 3, 5, etc., I get 125.0 as the value. But when I look in the SortedJsonData Array, I don’t get those values.

When I remove. iArrayPlace : INT;

TYPE typEnergyPrice :
STRUCT
	NOK_per_kWh		: REAL;
	EUR_per_kWh		: REAL;
	EXR				: REAL;
	time_start		: DATE_AND_TIME;
	time_end		: DATE_AND_TIME;
	NOK_per_kWh_mva : REAL;
//	iArrayPlace		: INT;
END_STRUCT
END_TYPE

AND the sorthing in HTTP_HvaKosterStrommen. I get the right vaules in tables inn HMI.

// Add array placement in array	
//	FOR i := 0 TO (n) DO
//		SortedJsonData[i].iArrayPlace := i;
//	END_FOR

I have tride to delete the tables in HMI and add it back. But I get the same result.

Code at GitLab

2 Likes

delete “{attribute ‘pack_mode’ := ‘0’}” from typEnergyPrice
image