Announcement

Collapse
No announcement yet.

Loop through JSON

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Loop through JSON

    I receive JSON messages, and need to loop through them to obtain certain values. The JSON could have many iterating elements. For example, on the below JSON, I need to capture values in
    AKA_Fname, AKA_Lname, AKA_Address1, AKA_City, AKA_State, AKA_Zip, TimestampUtc , if and only if the messageType = "ADT"

    Any ideas are welcome, thanks !



    [
    {
    "messages": {
    "itemNumber": "1",
    "itemConfig": null,
    "AKA::AKA_Fname": [
    "Apple",
    null,
    null
    ],
    "AKA::AKA_Lname": [
    "Pie",
    null,
    null
    ],
    "AKA::AKA_Address1": [
    null,
    "1 Big Farm",
    null
    ],
    "AKA::AKA_Address2": [
    null,
    null,
    null
    ],
    "AKA::AKA_City": [
    null,
    "Any City 1",
    null
    ],
    "AKA::AKA_State": [
    null,
    "Any State 1",
    null
    ],
    "AKA::AKA_Zip": [
    null,
    "000001",
    null
    ],
    "AKA::AKA_PhoneNumber": [
    "(000)000-0001",
    "(000)000-0002",
    null
    ]
    },
    "messageType": "ADT",
    "TimestampUtc": "2020-01-24T"
    },
    {
    "messages": {
    "itemNumber": "2",
    "itemConfig": null,
    "AKA::AKA_Fname": [
    "Banana",
    null,
    null
    ],
    "AKA::AKA_Lname": [
    "Cream",
    "",
    null
    ],
    "AKA::AKA_Address1": [
    null,
    "2 Big Farm",
    null
    ],
    "AKA::AKA_Address2": [
    null,
    null,
    null
    ],
    "AKA::AKA_City": [
    null,
    "Any City 2",
    null
    ],
    "AKA::AKA_State": [
    null,
    "Any State 2",
    null
    ],
    "AKA::AKA_Zip": [
    null,
    "00002",
    null
    ],
    "AKA::AKA_PhoneNumber": [
    "(000)000-0003",
    "(000)000-0004",
    null
    ]
    },
    "messageType": "ADT",
    "TimestampUtc": "2020-01-03T00"
    },
    {
    "messages": {
    "itemNumber": "3",
    "itemConfig": null,
    "AKA::AKA_Fname": [
    "Orange",
    null,
    null
    ],
    "AKA::AKA_Lname": [
    "Juice",
    "",
    null
    ],
    "AKA::AKA_Address1": [
    null,
    "3 Big Farm",
    null
    ],
    "AKA::AKA_Address2": [
    null,
    null,
    null
    ],
    "AKA::AKA_City": [
    null,
    "Any City 3",
    null
    ],
    "AKA::AKA_State": [
    null,
    "Any State 3",
    null
    ],
    "AKA::AKA_Zip": [
    null,
    "00003",
    null
    ],
    "AKA::AKA_PhoneNumber": [
    "(000)000-0005",
    "(000)000-0006",
    null
    ]
    },
    "messageType": "ORU",
    "TimestampUtc": "2020-02-03T00"
    }
    ]

  • #2
    Which Mirth version?
    Thanks.

    Jon

    Comment


    • #3
      Mirth Version 3.5.2 Thanks !

      Comment


      • #4
        I know of only two methods in the JSON object (stringify and parse), so if you have knowledge of other methods, please share them.

        From my perspective, I thought of flattening the JSON object by using the stringify method, then using a set of regular expressions in a hierarchical fashion to find the specific elements that you are looking for. In your example JSON object, regex for the "messages" name and, once you have found that, regex for the first child element ("AKA::AKA_Fname"), then grab all items within that set, parse and place them where they need to be, then regex for the next set.

        It takes a medium amount of thought, but may work. I also found a set of javascript functions at https://github.com/douglascrockford/JSON-js that appear to be pretty heavy duty, but do contain a couple of get functions for elements. Still trying to find the best way to use them.
        Thanks.

        Jon

        Comment


        • #5
          Using a regex to parse JSON (like using a regex to parse xml) is almost always the wrong choice and will likely get you into trouble.

          What do you mean by "capture the data?" What do you need to do with it? That is going to determine how you process the data. It's already captured in the JSON you are working with.

          Assuming you have your transformer data type set to JSON, msg will already contain the object that results from parsing your message. This is how you can quickly remove any elements from your array where messageType isn't ADT.
          Code:
          var msg = msg.filter(function(obj) {return obj.messageType == 'ADT'});

          Comment


          • #6
            Thanks guys, I found a way to get the items that I needed by doing this

            for(var i in msg)
            {
            var something = msg[i]['messages']['AKA::AKA_Fname'][0]
            .....
            ....

            }

            However, I'm now stuck on something else...

            The requirements have changed, and now I need to consider the "TimestampUtc" value. I would need to take all of the 'AKA::AKA_Fname', and if they are the same name , then ONLY take in the instance from the most recent TimestampUtc for that name.. then move on to the other 'AKA::AKA_Fname values, and do the same for all of them...

            Any ideas would be greatly appreciated.

            Comment

            Working...
            X