Jump to content

OpenJson UDF Library


Recommended Posts

OpenJson is a library that allows the extraction of data from text encoded in JSON format in a simple way.

#include "OpenJson.au3"

$json_text = FileRead('json.txt')
$dict = _OJParse($json_text)

; #GET COUNT OF ITEMS# ===========================================================================================================
ConsoleWrite('GET COUNT OF ITEMS' & @CRLF)
$count = $dict.Item('rows').Item(1).Item('elements').Count
ConsoleWrite('Items count: ' & $count & @CRLF & @CRLF)

; #SHOW SINGLE VALUES# ===========================================================================================================
ConsoleWrite('SHOW SINGLE VALUES' & @CRLF)
For $i = 1 To $count
    ConsoleWrite($dict.Item('rows').Item(1).Item('elements').Item($i).Item('duration').Item('text') & @CRLF)
Next
ConsoleWrite(@CRLF)

; #SHOW ALL KEYS# ===========================================================================================================
ConsoleWrite('SHOW ALL KEYS' & @CRLF)
$keys = $dict.Keys
For $k In $keys
    ConsoleWrite($k & @CRLF)
Next
ConsoleWrite(@CRLF)

; #SHOW ALL VALUES# ===========================================================================================================
ConsoleWrite('SHOW ALL VALUES' & @CRLF)
$items = $dict.Item('destination_addresses').Items
For $i In $items
    ConsoleWrite($i & @CRLF)
Next
ConsoleWrite(@CRLF)

; #CHECK IF KEY EXISTS# ===========================================================================================================
ConsoleWrite('CHECK IF KEY EXISTS' & @CRLF)
$exists = $dict.Item('rows').Item(1).Exists('elements')
ConsoleWrite("Exists key 'elements'?: " & $exists & @CRLF)


; #OUTPUT# ===========================================================================================================

;~ GET COUNT OF ITEMS
;~ Items count: 6

;~ SHOW SINGLE VALUES
;~ 3 hours 54 mins
;~ 1 hour 44 mins
;~ 1 day 18 hours
;~ 18 hours 43 mins
;~ 1 day 2 hours
;~ 1 day 18 hours

;~ SHOW ALL KEYS
;~ destination_addresses
;~ origin_addresses
;~ rows
;~ status

;~ SHOW ALL VALUES
;~ Washington, DC, USA
;~ Philadelphia, PA, USA
;~ Santa Barbara, CA, USA
;~ Miami, FL, USA
;~ Austin, TX, USA
;~ Napa County, CA, USA

;~ CHECK IF KEY EXISTS
;~ Exists key 'elements'?: True
; #OUTPUT# ===========================================================================================================

;==================================================================

The object returned by the _OJParse function contains 5 methods for extracting the information:

  1. Count: returns the number of items contained within an element
  2. Item('key')  or  Item(index): returns a specific item
  3. Keys: gets the list of keys for a specific element
  4. Items: gets the list of items of a specific element
  5. Exists('key'): or Exists(index): check if an element exists

;==================================================================

Elements must be chained to get sub elements:

Example:

File example.txt

{

"persons":[

        {"name":"Diego","age":27},

        {"name":"Juan","age":19},

        {"name":"Andres","age":43},

        {"name":"Emilio","age":35}

    ]

}

Autoit code:

#include "OpenJson.au3"

$json_text = FileRead('example.txt')
$dict = _OJParse($json_text)

$name = $dict.Item('persons').Item(3).Item('name')

ConsoleWrite($name)

OUTPUT:

Andres

;==================================================================

;There are three considerations that must be taken when using this library

1.- You should not to use double quotes within the texts.

Double quotes within a text should be avoided, otherwise the library will throw an error.

For example double quotes should not be in any of the following ways:

  •     "Hello" world"
  •     "hello "world""

But single quotes are allowed within the text:

  • "Hello 'world' "

2.- Be careful with the structure of the text in JSON format

Although it is obvious, it is worth remembering that if the provided JSON text structure has an error, the library will throw an error or return incomplete or empty data. This library uses the recursion process to build a tree of data in memory, so it is very sensitive to the quality of the JSON text that is processed. Please make sure that the JSON text to be processed is correct. None of the following five special characters must be missing or excess: [] {} "     Otherwise the library will throw an error, but be sure that if the text in JSON format is syntactically correct, the library will work correctly.

3.- Keys are case sensitive

Make sure the keys have the exact same name as supplied in the JSON-formatted text.

$dict.Item('name') is not the same as $dict.Item('Name')

;==================================================================

Three files are included in the attached archive:

  • OpenJson.au3  - The UDF Library
  • OJTest.au3  - Contains examples of the five functions available for manipulating JSON data
  • json.txt  - Contains sample text in JSON format

OpenJson_v1.2.zip

Edited by digrom
Link to post
Share on other sites
  • digrom changed the title to OpenJson UDF Library

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...