Giter Club home page Giter Club logo

autoit-json-udf's Introduction

Introduction

JSON is a pure data exchange format. Basically you only have to deal with JSON in 2 places in a program: Once when reading JSON data and once when outputting data. In between it should not really matter that the data used to be JSON or should be converted to it. You should not need any special intermediate structures but only the elements that the respective programming language provides anyway.

This is exactly the approach of this UDF: There is the function _JSON_Parse(), which converts an arbitrary JSON string into (nested) pure AutoIt data types (Arrays, Maps, Strings, Numbers, Null, True, False). And on the other side we have the function _JSON_Generate(), which generates a JSON string from arbitrary (nested) AutoIt data structures.

Import and export JSON

So how to use - let`s give an example:

#include "JSON.au3"

Global $s_String = '[{"id":"4434156","url":"https://legacy.sky.com/v2/schedules/4434156","title":"468_CORE_1_R.4 Schedule","time_zone":"London","start_at":"2017/08/10 19:00:00 +0100","end_at":null,"notify_user":false,"delete_at_end":false,"executions":[],"recurring_days":[],"actions":[{"type":"run","offset":0}],"next_action_name":"run","next_action_time":"2017/08/10 14:00:00 -0400","user":{"id":"9604","url":"https://legacy.sky.com/v2/users/9604","login_name":"[email protected]","first_name":"Robin","last_name":"John","email":"[email protected]","role":"admin","deleted":false},"region":"EMEA","can_edit":true,"vm_ids":null,"configuration_id":"19019196","configuration_url":"https://legacy.sky.com/v2/configurations/19019196","configuration_name":"468_CORE_1_R.4"},{"id":"4444568","url":"https://legacy.sky.com/v2/schedules/4444568","title":"468_CORE_1_R.4 Schedule","time_zone":"London","start_at":"2017/08/11 12:00:00 +0100","end_at":null,"notify_user":false,"delete_at_end":false,"executions":[],"recurring_days":[],"actions":[{"type":"suspend","offset":0}],"next_action_name":"suspend","next_action_time":"2017/08/11 07:00:00 -0400","user":{"id":"9604","url":"https://legacy.sky.com/v2/users/9604","login_name":"[email protected]","first_name":"Robin","last_name":"John","email":"[email protected]","role":"admin","deleted":false},"region":"EMEA","can_edit":true,"vm_ids":null,"configuration_id":"19019196","configuration_url":"https://legacy.sky.com/v2/configurations/19019196","configuration_name":"468_CORE_1_R.4"}]'

; ================= parse the JSON-String into a nested AutoIt data structure ==============
$o_Object = _JSON_Parse($s_String)

; ================= query values from the structure directly with AutoIt syntax ============
$s_Type = $o_Object[1].actions[0].type
ConsoleWrite("type: " & $s_Type & @CRLF)

;  ; ================= query values via _JSON_Get() (safer and clearer) =======================
$s_Type = _JSON_Get($o_Object, "[1].actions[0].type")
ConsoleWrite("type: " & $s_Type & @CRLF & @CRLF)

;  ; ================= convert AutoIt data structures into a JSON string ======================
ConsoleWrite(_JSON_Generate($o_Object) & @CRLF & @CRLF)
;  ; compact form:
ConsoleWrite(_JSON_Generate($o_Object, "", "", "", "", "", "") & @CRLF & @CRLF)

Handling nested data structures

JSON is often very nested. The resulting AutoIt data is therefore naturally also nested, which makes it somewhat cumbersome to process with pure AutoIt on-board methods.

For this reason, the UDF comes with a few helper functions that make life with this data easier. One of them is _JSON_Get(), which allows you to access deeply nested data with a simple query syntax. On the other hand there is the function _JSON_addChangeDelete() with which you can (the name already says it) change, add and delete data. You can even easily create deeply nested structures with a single call.

Again, here is a small example of how to use it:

#include "JSON.au3"

Global $mMap ; target variable

; Create a structure to manage the employees of different companies in their respective company sites:
_JSON_addChangeDelete($mMap, "our company.company sites[1].employee[0]", "John Johnson")
_JSON_addChangeDelete($mMap, "our company.company sites[1].employee[1]", "Margret Margretson")
_JSON_addChangeDelete($mMap, "our company.company sites[3].employee[0]", "Betty Bettinson")

; Change a value - e.g. replace the employee "John Johnson"
_JSON_addChangeDelete($mMap, "our company.company sites[1].employee[0]", "Mark Marcusson")

; delete the second employee in the 2nd site ("Margret Margretson")
_JSON_addChangeDelete($mMap, "our company.company sites[1].employee[1]")

; show the resulting data structure
ConsoleWrite(_JSON_Generate($mMap) & @CRLF & @CRLF)

Strictly speaking, these functions should not even have "JSON" in their names, since they are generally applied to data structures in AutoIt. However, since they are often used in the JSON environment, we allow ourselves this small inaccuracy.

autoit-json-udf's People

Contributors

sven-seyfert avatar sylvan86 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.