Dehydrate (serialize) as JSON

Verified
Added by iNTERFACEWARE

How to dehydrate (serialize) raw JSON data, this can be useful for storing in a databases

Source Code
 function main(Data)   
  -- serialize data as JSON
   
   -- this is the original JSON data:
   -- "{'int_test': 1.23, 'string_test':'a', 'boolean_test' : true}"
   
   -- JSON data as a Lua table
   local JT = {['int_test']=1.23, ['string_test']='a', ['boolean_test']=true}

   -- serialize the Lua table into JSON
   local JS = json.serialize{data=JT}   
   --> '{ "int_test": 1.230000, "boolean_test": true, "string_test": "a" }'
   -- notice the changed order and added decimal places for int_test
   -- when compared to our original JSON data:
   -- "{'int_test': 1.23, 'string_test':'a', 'boolean_test' : true}"
   
   -- parse the JSON string back to a Lua table for comparison
   local JT = json.parse{data=JS}
   --> {['int_test']=1.230000, ['boolean_test']=true, ['string_test']='a'}
   -- again notice the changed order and added decimal places for int_test
   -- when compared to our original table:
   -- {['int_test']=1.23, ['string_test']='a', ['boolean_test']=true}
end
Description
How to dehydrate (serialize) raw JSON data, this can be useful for storing in a databases
Usage Details

Demonstrates how to serialize data (in a Lua table) as JSON, by using json.serialize{}. This can be very useful for storing raw data in a database, as opposed than breaking it out and saving it in various tables.

How to use the snippet:

  • Paste the code into your script
  • Inspect the annotations to see how it works