Automic Workload Automation

Expand all | Collapse all

Reading JSON object ?

  • 1.  Reading JSON object ?

    Posted 04-03-2017 07:49 AM
    Hi

    I have an external JSON object. For example;
    { "firstname":"John", "lastname":"Carter", "age":30 }
    Is there a possibilities to use this in a JOB or SCRIPT.

    :SET &JSON_OBJECT#={ "firstname":"John", "lastname":"Carter", "age":30 }
    :SET &FIRST# = GET_JSON_VAR(&JSON_OBJECT#,"firstname")
    :SET &LAST# = GET_JSON_VAR(&JSON_OBJECT#,"lastname")
    :SET &AGE# = GET_JSON_VAR(&JSON_OBJECT#,"age")

    I know there isn't a function like GET_JSON_VAR
    But is there another way to 'read' items within a JSON object

    Thanks in advanced


  • 2.  Reading JSON object ?

    Posted 04-03-2017 08:16 AM
    Probably the simplest way would be to create a generic include "JSON reader" AE script that could have a JSON passed in, read (presumably by taking off the brackets and then splitting on commas), and then having variables generated based on the number of values read. 

    It obviously matters how you want to use this data, but if you think this will be of constant use making a generic is a good idea. 


  • 3.  Reading JSON object ?

    Posted 04-03-2017 10:00 AM
    Out of the box there is no script function or similar for the JSON format.

    As Michael suggested I am afraid too you should start createing a generic converter from scratch.

    Another possibility that might work if you find a batch- usable converter tool and create a generic job for this task.



  • 4.  Reading JSON object ?



  • 5.  Reading JSON object ?

    Posted 04-03-2017 12:19 PM
    Here's an example AE script to read in a value (in this example passed as a variable of the job itself):

    :SET &JSON_PARSE# = STR_CUT(&JSON#,3)
    :SET &JSON_LENGTH# = STR_LENGTH(&JSON_PARSE#)
    :SET &JSON_LENGTH# = &JSON_LENGTH# - 2
    :SET &JSON_LENGTH# = FORMAT(&JSON_LENGTH#,)
    :SET &JSON_PARSE# = STR_CUT(&JSON_PARSE#,1,&JSON_LENGTH#)

    :DEFINE &JSON_VALUES#, STRING, 99
    :DEFINE &JSON_VAL#, STRING, 2
    :FILL &JSON_VALUES#[] = STR_SPLIT(&JSON_PARSE#,", """)
    :SET &ARRAY_NUM# = 1
    :WHILE &JSON_VALUES#[&ARRAY_NUM#] <> ""
    :  IF &ARRAY_NUM# <> 1
    :    SET &JSON_VALUES#[&ARRAY_NUM#] = STR_CAT("""",&JSON_VALUES#[&ARRAY_NUM#])
    :  ENDIF
    :  FILL &JSON_VAL#[] = STR_SPLIT(&JSON_VALUES#[&ARRAY_NUM#],":")
    :  PRINT &JSON_VAL#[1] &JSON_VAL#[2]
    :  SET &ARRAY_NUM# = &ARRAY_NUM# + 1
    :ENDWHILE

    (Apparently they changed how the Code block works, it reads in AE scripting weirdly and adds extra lines now, pasted as plaintext instead)

    Given the format you provided, this should correctly parse your data, though in its current form the value name still contains quotations and the "age" is set as a String (though this can easily be converted to a number with a simple IS_NUMERIC check and then a CONVERT).  Bear in mind this example is fairly basic and may provide false positives if your data contains ', "'.

    Result:
    2017-04-03 16:11:22 - U00020206 Variable '&JSON#' was stored with value '{ "firstname":"John", "lastname":"Carter", "age":30 }'.
    2017-04-03 16:11:22 - U00020408 "firstname" "John"
    2017-04-03 16:11:22 - U00020408 "lastname" "Carter"
    2017-04-03 16:11:22 - U00020408 "age" 30



  • 6.  Reading JSON object ?

    Posted 04-26-2017 02:32 AM
    Hi,

    Tnx for the comments (and sorry for the delayed reaction.)
    But I think the example Michael is a good base to work this out for my situation.




  • 7.  Re: Reading JSON object ?

    Posted 05-16-2019 12:40 PM

    The upcoming v12.3 will have JSON functions added to UC4script.