Page 1 of 1

WAMs - Reading JSON data

Posted: Thu Jan 09, 2020 9:31 pm
by jan
Good day,

I have an HttpRequest that responds with a JSON object that looks like this:

Code: Select all

{
    "searchid": "4",
    "squishvin": null,
    "year": "2016",
    "make": "Cadillac",
    "model": "CT6",
    "body": null,
    "helptip": null,
    "meta": null,
    "parts": [
        {
            "id": "2539",
            "part_number": "DW02305",
            "glass": "Windshield",
            "glass_type_id": "1",
            "description": "DW02305 2016-2019 CADILLAC CT6 4D SED ACI FCA LDWS LKA RS SOL",
            "count": "2",
            "dealer_part_nums": [
                "84219866"
            ]
        },
        {
            "id": "2931",
            "part_number": "DW02370",
            "glass": "Windshield",
            "glass_type_id": "1",
            "description": "DW02370 2016-2019 CADILLAC CT6 4D SED ACI FCA HUD LDWS LKA RS SOL",
            "count": "2",
            "dealer_part_nums": [
                "23346192",
                "84219867"
            ]
        }
    ]
}
I need to read the data inside the "parts" array. Which JSON Reader should I be using? I am doing this inside a WAM. Thanks.

Re: WAMs - Reading JSON data

Posted: Thu Jan 30, 2020 4:05 am
by MarcusLancaster
Hi Jan

Have you been able to solve this? If not I've got some code that might help - I've contrived a web service to return your sample JSON and have a basic parser which picks out data from each part.

Cheers for now.

Marcus.

Re: WAMs - Reading JSON data

Posted: Thu Feb 13, 2020 5:25 pm
by jan
Hi Marcus,

Yes I was able to solve it but the solution might not be so efficient. Here is the code I used below:

Code: Select all

#JsonRandomAccessReader.SetSourceHttpResponse Httpresponse(#HttpRequest.Response) Errorinfo(#JsonErrorInfo)

If Cond(#JsonErrorInfo.OK)

	#FXRETCD := 'NR' /* Set to no parts found right here */

	For Each(#Part) In(#JsonRandomAccessReader.ReadObjectWithName( "parts" ))

		#FXRETCD := 'OK'

		If Cond(#Part.isObject)

			#RootObject.ParseString String(#Part.asString) Errorinfo(#JsonErrorInfo)

			If Cond(#JsonErrorInfo.OK)

				#LW3ITEMCD := #RootObject.GetString( 'part_number' ).AsNativeString.Trim
				#QSRKEY := #LW3ITEMCD.Trim.Reverse.Substring( 1 4 ).Reverse
				Add_Entry To_List(#LS_PARTS)

			Endif

		Endif

	Endfor

Endif
Maybe your example is better? If so, could you share it? (Currently stuck again with another JSON response that has a different format).

Re: WAMs - Reading JSON data

Posted: Thu Feb 13, 2020 7:07 pm
by MarcusLancaster
Hi Jan

No problem - so on the server I run the API to return your JSON. The first step after that (after testing HTTP response codes etc) is to load that response into a JSON Document;

#JSONDocument.LoadFromString(#HttpRequest.Response.AsString)

Then I use a FOR loop to read through the JSON document, looking for the PARTS structure. Once I've found that, another FOR loop processes that inner structure grabbing (in this case) the id and part_number and writes a record to my working list #PartsList.

For Each(#element) In(#JSONDocument.rootnode)

If (#element.nodename.uppercase = PARTS)

Set_Ref Com(#JSONMember) To(*dynamic #element)

For Each(#item) In(#JSONMember.Value)
#std_text := #item<'id'>.asstring
#std_textl := #item<'part_number'>.asstring
Add_Entry To_List(#PartsList)
endfor

endif

Endfor


Obviously there's a few things defined at the top of the program; the list definition;

def_list #PartsList fields(#std_text #std_textl) type(*working) Entrys(*MAX)

and relevant HTTP / JSON components;

Define_Com Class(#PRIM_JSON.Document) Name(#JSONDocument)
Define_Com Class(#XPRIM_HttpRequest) Name(#HttpRequest)
Define_Com Class(#PRIM_JSON.Member) Name(#JSONMember) Reference(*DYNAMIC)


I think that's everything. Hope that helps.

Marcus.

Re: WAMs - Reading JSON data

Posted: Tue Feb 18, 2020 1:18 pm
by jan
Thanks for this Marcus. Very interesting as I have never used JSONDocument or JSONMember before.

I'll definitely give this a shot when an opportunity arrives.