The parse_json
function parses a string of JSON and returns a JSON value.
parse_json( json )
Argument | Description |
---|---|
json
|
(string) The input string to parse. |
(LuaJsonValue). A LuaJsonValue containing the JSON data.
local fh = io.open("example_json.json", "r") local file_content = fh:read("*all") fh:close() local myJsonValue = parse_json(file_content) if myJsonValue:is_object() then document:insertJson( myJsonValue:object() , "MyJsonField" ) elseif myJsonValue:is_array() then document:insertJson( myJsonValue:array() , "MyJsonField" ) end
If the file example_json.json
contained the following JSON object:
{ "product": "IDOL", "component": "CFS", "version": { "major": 11, "minor": 3 }, "ports": [ { "type": "ACI", "number": 7000 }, { "type": "Service", "number": 17000 } ] }
The resulting document might look like this:
<document> <reference>ref1</reference> <xmlmetadata> <MyJsonField> <component>CFS</component> <ports> <number>7000</number> <type>ACI</type> </ports> <ports> <number>17000</number> <type>Service</type> </ports> <product>IDOL</product> <version> <major>11</major> <minor>3</minor> </version> </MyJsonField> </xmlmetadata> </document>
If you know that the input data is a JSON object, you can also use the function parse_json_object. If you know that the input data is a JSON array, you can also use the function parse_json_array.
|