Automic Workload Automation

Expand all | Collapse all

Reading JSON object ?

  • 1.  Reading JSON object ?

    Posted 04-03-2017 07:49 AM

    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")

    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 &ARRAY_NUM# = 1
    :  IF &ARRAY_NUM# <> 1
    :  ENDIF
    :  PRINT &JSON_VAL#[1] &JSON_VAL#[2]
    :  SET &ARRAY_NUM# = &ARRAY_NUM# + 1

    (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 ', "'.

    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

    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.