Difference between revisions of "Action: JSON Parse"
Endercraft (talk | contribs) Tags: Mobile web edit Mobile edit |
|||
Line 40: | Line 40: | ||
</pre> | </pre> | ||
You can then use the values of interest from the dictionary in your macros, for example to populate a weather summary from a weather API or similar. | You can then use the values of interest from the dictionary in your macros, for example to populate a weather summary from a weather API or similar. | ||
+ | |||
+ | A more complex example: | ||
+ | <pre> | ||
+ | |||
+ | { | ||
+ | "Body" : { | ||
+ | "Data" : { | ||
+ | "Inverters" : { | ||
+ | "1" : { | ||
+ | "Battery_Mode" : "suspended", | ||
+ | "DT" : 83, | ||
+ | "E_Day" : 215.89999389648438, | ||
+ | "E_Total" : 7790457, | ||
+ | "E_Year" : 2533685, | ||
+ | "P" : 32, | ||
+ | "SOC" : 6.8000001907348633 | ||
+ | } | ||
+ | }, | ||
+ | "Site" : { | ||
+ | "BatteryStandby" : true, | ||
+ | "E_Day" : 215.90000000000001, | ||
+ | "E_Total" : 7790456.9000000004, | ||
+ | "E_Year" : 2533685, | ||
+ | "Meter_Location" : "grid", | ||
+ | "Mode" : "bidirectional", | ||
+ | "P_Akku" : 0, | ||
+ | "P_Grid" : 371.16000000000003, | ||
+ | "P_Load" : -403.16000000000003, | ||
+ | --> "P_PV" : 55.719999999999999, | ||
+ | "rel_Autonomy" : 7.9372953666038288, | ||
+ | "rel_SelfConsumption" : 100 | ||
+ | }, | ||
+ | "Version" : "12" | ||
+ | } | ||
+ | }, | ||
+ | "Head" : { | ||
+ | "RequestArguments" : {}, | ||
+ | "Status" : { | ||
+ | "Code" : 0, | ||
+ | "Reason" : "", | ||
+ | "UserMessage" : "" | ||
+ | }, | ||
+ | "Timestamp" : "2023-11-28T14:47:38+01:00" | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | The above JSON table is used in this example. The value which will be selected is P_PV, the one marked by --> in the printout. | ||
+ | |||
+ | If the JSON content is stored in local string variable Vjson click ‘Parse JSON’ from the ‘Add Action’ menu, part ‘MacroDroid Specific’ and choose Vjson as ‘Input String Variable’ and Djson as ‘Output Dictionary Variable’ by click on the + symbol. Thereafter Djson can be put to live by a click on ‘JSON Parse’ in the Action window and a further click on ‘Test action’ in the new opened window. | ||
+ | |||
+ | A click on Djson in ‘Local Variables’ at the bottom of the screen shows two entries: ‘Body’ with one entry and ‘Head’ with two entries. A click on ‘Body’ opens a new window with three new entries: ‘Inverters’, ‘Site’ and ‘Version’, each coming with their number of new sub entries. A click on ‘Site’ opens in this case 12 values, each already analysed by content (Boolean, Decimal, Integer and String). Compare it with the printout to get an idea of the JSON structure. | ||
+ | |||
+ | The next action after ‘JSON Parse’ is ‘Set Variable’ from the ‘Add Action’ menu part ‘MacroDroid Specific’. Give it a name for instance ‘PV’ but never P_PV to avoid conflicts. | ||
+ | As P_PV is numeric either Integer (if fractions are of no importance) or Decimal has to be selected. Close the window and select ‘Expression’ in the next window. Click in the blue field right to the ‘Enter value’. In the new window select ‘LocalVar:Djson’ as option. After OK a window ‘Select Dictionary Entry’ opens showing ‘Define Manually’ (for experienced users) and the two main entries [Body] and [Head] of the JSON table. Select Djson[Body]. Proceed to Djson[Body][Data], Djson[Body][Data][Site] and finally select Djson[Body][Data][Site][P_PV]. The ‘Set Variable’ row in the Action window reads after this | ||
+ | |||
+ | PV:{lv=Djson[Body][Data][Site][P_PV]}. | ||
+ | |||
+ | To verify the total action click in the ‘Actions’ window on ‘Set Variable’ and click then on ‘Test Action’. If all went correct the Variable PV in the ‘LocalVariables’ window will show 58, omitting the fractions because PV was declared as integer. Be aware that there might be a problem with the decimal point because in some countries a comma is use instead. |
Revision as of 14:34, 28 November 2023
This action enables structured data in JSON format from a String variable to be parsed into a MacroDroid Dictionary variable. It will typically be used in conjunction with the HTTP Request action which will save the JSON data into a string ready to be parsed.
JSON data is a widely used format by many Web APIs to provide data in a structured/nested format. A typical example of such data is below:
{ "firstName": "Rajesh", "lastName": "Kumar", "gender": "man", "age": 24, "address": { "streetAddress": "126 Udhna", "city": "Surat", "state": "GJ", "postalCode": "394221" }, "phoneNumbers": [ { "type": "home", "number": "7383627627" } ] }
This data can be obtained from the following url: https://tools.learningcontainer.com/sample-json.json
This data will be parsed into a dictionary variable in MacroDroid and end up populated with the data below:
dictionary[firstName] = "Rajesh" dictionary[lastName] = "Kumar" dictionary[gender] = "man" dictionary[age] = 24 dictionary[address] = sub-dictionary with 4 entries dictionary[address][streetAddress] = "126 Udhna" dictionary[address][city] = "Surat" dictionary[address][state] = "GJ" dictionary[address][postalCode] = "394221" dictionary[phoneNumbers] = sub-array with 2 entries dictionary[phoneNumbers][0] = sub-dictionary with 2 entries dictionary[phoneNumbers][type] = "home" dictionary[phoneNumbers][number] = 7383627627
You can then use the values of interest from the dictionary in your macros, for example to populate a weather summary from a weather API or similar.
A more complex example:
{ "Body" : { "Data" : { "Inverters" : { "1" : { "Battery_Mode" : "suspended", "DT" : 83, "E_Day" : 215.89999389648438, "E_Total" : 7790457, "E_Year" : 2533685, "P" : 32, "SOC" : 6.8000001907348633 } }, "Site" : { "BatteryStandby" : true, "E_Day" : 215.90000000000001, "E_Total" : 7790456.9000000004, "E_Year" : 2533685, "Meter_Location" : "grid", "Mode" : "bidirectional", "P_Akku" : 0, "P_Grid" : 371.16000000000003, "P_Load" : -403.16000000000003, --> "P_PV" : 55.719999999999999, "rel_Autonomy" : 7.9372953666038288, "rel_SelfConsumption" : 100 }, "Version" : "12" } }, "Head" : { "RequestArguments" : {}, "Status" : { "Code" : 0, "Reason" : "", "UserMessage" : "" }, "Timestamp" : "2023-11-28T14:47:38+01:00" } }
The above JSON table is used in this example. The value which will be selected is P_PV, the one marked by --> in the printout.
If the JSON content is stored in local string variable Vjson click ‘Parse JSON’ from the ‘Add Action’ menu, part ‘MacroDroid Specific’ and choose Vjson as ‘Input String Variable’ and Djson as ‘Output Dictionary Variable’ by click on the + symbol. Thereafter Djson can be put to live by a click on ‘JSON Parse’ in the Action window and a further click on ‘Test action’ in the new opened window.
A click on Djson in ‘Local Variables’ at the bottom of the screen shows two entries: ‘Body’ with one entry and ‘Head’ with two entries. A click on ‘Body’ opens a new window with three new entries: ‘Inverters’, ‘Site’ and ‘Version’, each coming with their number of new sub entries. A click on ‘Site’ opens in this case 12 values, each already analysed by content (Boolean, Decimal, Integer and String). Compare it with the printout to get an idea of the JSON structure.
The next action after ‘JSON Parse’ is ‘Set Variable’ from the ‘Add Action’ menu part ‘MacroDroid Specific’. Give it a name for instance ‘PV’ but never P_PV to avoid conflicts. As P_PV is numeric either Integer (if fractions are of no importance) or Decimal has to be selected. Close the window and select ‘Expression’ in the next window. Click in the blue field right to the ‘Enter value’. In the new window select ‘LocalVar:Djson’ as option. After OK a window ‘Select Dictionary Entry’ opens showing ‘Define Manually’ (for experienced users) and the two main entries [Body] and [Head] of the JSON table. Select Djson[Body]. Proceed to Djson[Body][Data], Djson[Body][Data][Site] and finally select Djson[Body][Data][Site][P_PV]. The ‘Set Variable’ row in the Action window reads after this
PV:{lv=Djson[Body][Data][Site][P_PV]}.
To verify the total action click in the ‘Actions’ window on ‘Set Variable’ and click then on ‘Test Action’. If all went correct the Variable PV in the ‘LocalVariables’ window will show 58, omitting the fractions because PV was declared as integer. Be aware that there might be a problem with the decimal point because in some countries a comma is use instead.