Skip to content

Object Json

Name Input Params Return Data Description
json_getfield String, String String, String Get the value of the selected Json Field
json_arraylength String, String Integer, String Get the length of the selected Json Array Field

json_getfield(String, String)

Get the value of the selected Json Field.
This function can elaborate well-structured json, with multiple subfields and arrays.
To access the values of a sub array, the field must be "Field.SubField", in this way the function will take all the values within the first index of the sub array.
Subsequently, to take the values of sub arrays of sub arrays, the Field has to be "Field.Index.SubField", "Field.SubField.Index" and so on.

Example
Value, err = json_getfield(Json, Field)
Input Parameters
 - Json (String): The Json that contains the values.
 - Field (String): The interested field in the Json.
Returns
 - Value (String): The function returns the Value of the selected Field in the Json.
 - err (String): The function returns the error if is it present.

Json = '{"name":"John", "age":30, "cars": [{"Bmw":"M4", "Audi":"A3", "Volkswagen":"Golf"}, {"Lamborghini":"Murcielago", "Ferrari":"F40", "Pagani":"Huayra"}, {"Suzuki":"Vitara", "Nissan":"GTR R33", "Subaru":"Impreza WRX"}]}'
                -- The Json that contains the values
Field = "cars"  -- The interested field in the Json
Value, err = json_getfield(Json, Field)
if err ~= "" then
    -- error 
    resiot_debug(err)
else
    -- value read correctly
    resiot_debug(Value)
end
Field = "cars.1"    -- The interested field in the Json
Value, err = json_getfield(Json, Field)
if err ~= "" then
    -- error 
    resiot_debug(err)
else
    -- value read correctly
    resiot_debug(Value)
end
Field = "cars.1.Ferrari"    -- The interested field in the Json
Value, err = json_getfield(Json, Field)
if err ~= "" then
    -- error 
    resiot_debug(err)
else
    -- value read correctly
    resiot_debug(Value)
end
Field = "cars.2.Toyota"     -- The interested field in the Json
Value, err = json_getfield(Json, Field)
if err ~= "" then
    -- error 
    resiot_debug(err)
else
    -- value read correctly
    resiot_debug(Value)
end

Return to index


json_arraylength(String, String)

Get the length of the selected Json Array Field.
Given a json containing an array, if we follow the same reasoning as the "json_getfield" function to return the value of an array, this function will return the length of that array instead.
If the length of something other than an array is requested as input, this function will return an error.

Example
Length, err = json_arraylength(Json, Field)
Input Parameters
 - Json (String): The Json that contains the array.
 - Field (String): The interested array in the Json.
Returns
 - Length (Integer): The function returns the Length of the selected Array in the Json.
 - err (String): The function returns an error in case of invalid input or internal error.

Json = '{"name":"John", "age":30, "cars": [{"Bmw":"M4", "Audi":"A3", "Volkswagen":"Golf"}, {"Lamborghini":"Murcielago", "Ferrari":"F40", "Pagani":"Huayra"}, {"Suzuki":"Vitara", "Nissan":"GTR R33", "Subaru":"Impreza WRX"}]}'
                -- The Json that contains the values
Field = "cars"  -- The interested array in the Json
Length, err = json_arraylength(Json, Field)
if err ~= "" then
    -- error 
    resiot_debug(err)
else
    -- length read correctly
    resiot_debug(Length)
end

Return to index