Maximo Open Forum

  • 1.  Autoscript - how to handle a json object with objects

    Posted 12-01-2023 16:50

    I've been given an json string that the simplified version looks like this

    {
        "DATA_DS": {
            "P_END_DATE": "2023-11-30T00:00:00.000+00:00",
            "G_1": {
                "VALIDATION_STATUS": "APPROVED",
                "INVOICE_NUMBER": "INV_PO.060.60",
                "INVOICE_LINENUMBER": "1",
            },
            "G_1": {
                "VALIDATION_STATUS": "APPROVED",
                "INVOICE_NUMBER": "INV_PO.060.60",
                "INVOICE_LINENUMBER": "2",
            }
        }
    }

    And via an autoscript I need to process each G_1 object.

    I've tried 

    from com.ibm.json.java import JSONObject, JSONArray

    message= JSONObject.parse(inputString)
    log("message=" + str(message))

    And the following is logged (it only has the first G_1 object, it's missing the second)

    {
        "DATA_DS": {
            "P_END_DATE": "2023-11-30T00:00:00.000+00:00",
            "G_1": {
                "VALIDATION_STATUS": "APPROVED",
                "INVOICE_NUMBER": "INV_PO.060.60",
                "INVOICE_LINENUMBER": "1",
            },

    }

    }

    How can I process this collection of objects inside an object?  I need to get to each member of each G_1 object.

    (Maximo 7.6.1.2


    #Customizations
    #Integrations

    ------------------------------
    Jose Nodeland
    JedaWorks
    ------------------------------


  • 2.  RE: Autoscript - how to handle a json object with objects

    Posted 12-02-2023 06:47
    You'll need something like this as per the JSON String structure -
    parsed_data = json.loads(<YOUR JSON STRING>)
    g_1_value = parsed_data["DATA_DS"]["G_1"]


    ------------------------------
    Prashant Sharma
    Sedin Technologies
    Connect with me @ https://www.linkedin.com/in/psharmamaximo/

    #IBM Champion 2022
    #IBM Champion 2023
    ------------------------------



  • 3.  RE: Autoscript - how to handle a json object with objects

    Posted 12-03-2023 08:51
    Still having issues with this.  My python autoscript looks like

    from com.ibm.json.java import JSON
    parsed_data = JSON.loads(inputString)

    Which returns error
    type object 'com.ibm.json.java.JSON' has no attribute 'loads'

    --------------------------------------------------------------------------

    If the code is changed to
    from com.ibm.json.java import JSON
    parsed_data = JSON.parse(inputString)

    Only the first G_1 object is returned.

    ---------------------------------------------------------------------------

    I'm importing the wrong JSON?







  • 4.  RE: Autoscript - how to handle a json object with objects

    Posted 12-04-2023 08:26

    Hi,

    Your JSON is malformed. You can't have two attributes with the same name. If you need two values under the same name, you should put them into an array.



    ------------------------------
    Andreas Brieke
    SVA System Vertrieb Alexander GmbH
    ------------------------------



  • 5.  RE: Autoscript - how to handle a json object with objects

    Posted 12-04-2023 10:42

    To expand on Andreas's response, he is correct that your JSON example is malformed in that it has two properties with the same name. Your choice is either to place the G_1 objects in an array like the following:

    {
        "DATA_DS": {
            "P_END_DATE": "2023-11-30T00:00:00.000+00:00",
            "G_1": [
                {
                    "VALIDATION_STATUS": "APPROVED",
                    "INVOICE_NUMBER": "INV_PO.060.60",
                    "INVOICE_LINENUMBER": "1"
                },
                {
                    "VALIDATION_STATUS": "APPROVED",
                    "INVOICE_NUMBER": "INV_PO.060.60",
                    "INVOICE_LINENUMBER": "2"
                }
            ]
        }
    }

    Or you can uniquely name the G_1 objects, such as G_1, G_2, G_3 etc.

    {
        "DATA_DS": {
            "P_END_DATE": "2023-11-30T00:00:00.000+00:00",
            "G_1": {
                "VALIDATION_STATUS": "APPROVED",
                "INVOICE_NUMBER": "INV_PO.060.60",
                "INVOICE_LINENUMBER": "1"
            },
            "G_2": {
                "VALIDATION_STATUS": "APPROVED",
                "INVOICE_NUMBER": "INV_PO.060.60",
                "INVOICE_LINENUMBER": "2"
            }
        }
    }

    I find working with JSON in Jython to be annoying at best, if you use JavaScript it just natively handles JSON (I mean it's in the name JavaScript Object Notation), but if you are using Python then you want to use the following. Note: I think Prashant had a typo of "loads" vs "parse".

    from com.ibm.json.java import JSONObject
    
    # This returns a JSONObject instead of the more generic JSONArtifact that the JSON.parse() function returns.
    data = JSONObject.parse(inputString)
    
    # Array processing
    for g1 in data.get("G_1"):
       # do your processing here.

    Documentation for the com.ibm.json.java package is here for your reference https://www.ibm.com/docs/en/content-navigator/3.0.x?topic=reference-comibmjsonjava



    ------------------------------
    Jason VenHuizen
    Sharptree
    ------------------------------



  • 6.  RE: Autoscript - how to handle a json object with objects

    Posted 12-08-2023 05:19

    I had the supplier of the json modify as suggested, and all is good.

    Thanks everyone.



    ------------------------------
    Jose Nodeland
    JedaWorks
    ------------------------------