troubleshooting Question

How to Modify a JSON Array without Compromising the format/structure

Avatar of Rick Becker
Rick BeckerFlag for United States of America asked on
Visual Basic.NETJSON
4 Comments1 Solution64 ViewsLast Modified:
Greeting... Again

I recently posted a Question on EE and received a great and quick answer .. "Proper JSON Syntax for extracting Data from Nested Arrays" this allowed me to continue with a little project that I am working on..

My issue now is that I need to modify the contents of a JSON file after it has been loaded into a Json Array. After the Json Array has been modified/updated I need to 'POST' it back to the server. The service that I POST to will send back a 'Successful Upload' message with an unmodified Json Array but Fails when I attempt to modify the Array and send it back up.

As I mentioned in my previous Question I am just now trying to learn how to use Json so I am completely in the dark about most of it.. The code that I have is attached  has 2 functions, One that pulls and Order File and attempts to modify it's contents and the Second POST's it back again...

I am 100% sure that I am NOT using the right functionality to modify the contents of the Array so a few pointers in the right direction would be most helpful...

Thanks in advance,
Rick

EncodedShipStationID = EncodeBase64(ShipStationAppID)

        ' ShipStation api call... 
        '##############################################################
        sContactURL = "https://ssapi.shipstation.com/orders?ordernumber=1168501&orderStatus=awaiting_shipment"
        '"orderNumber":"1168501","

        Call xmlHttp2.open("GET", sContactURL, False)
       
        xmlHttp2.setRequestHeader("Authorization:", " Basic " + EncodedShipStationID) 'MsgBox("Here 1 in FindItemsByProduct")

        Call xmlHttp2.send()

        'Get the server's response
        sResponse = xmlHttp2.responseText
        sResponse = Trim(sResponse)

        'Save the response from shipstation for later evaluation if needed
        RtnVal = VarStringToTextFile(TempDataStorageFolder + "\ShipStationOrderResponse1.xml", sResponse)

        ' Now parse out Item Data using JSON parser
        Dim intI As Integer
        Dim json As String = sResponse.ToString
        Dim jsonObject As Newtonsoft.Json.Linq.JObject = Newtonsoft.Json.Linq.JObject.Parse(json)
        Dim jsonArray As Newtonsoft.Json.Linq.JArray = jsonObject("orders")

        Dim CurrentSKU As String
        Dim CurrentOption As String
        Dim OrderProcessed As String
        Dim NewJasonRecord As String

        CurrentSKU = ""
        CurrentOption = ""
        OrderProcessed = ""

        For intI = 0 To jsonArray.Count - 1
            ' first test to see if this Order has already been processes if it has then
            ' just go to the next order

            For Each newoption In jsonObject.SelectTokens("$.orders[" + CStr(intI) + "].advancedOptions.customField1")

                OrderProcessed = newoption.ToString
                If OrderProcessed = "" Then

                    ' if not processed the look for the parent sku
                    For Each item In jsonObject.SelectTokens("$.orders[" + CStr(intI) + "].items[*].sku")
                        'Console.WriteLine(item.ToString & "; ")
                        CurrentSKU = CurrentSKU + item.ToString + ","
                    Next

                    'add 'Processed' to the advancedOptions.customField1 field
                    'NewJasonRecord = jsonArray(intI).Replace(Chr(34) + "customField1" + Chr(34) + ":null", Chr(34) + "customField1" + Chr(34) + ":null")
                    NewJasonRecord = jsonArray(intI).ToString
                    NewJasonRecord = NewJasonRecord.Replace(Chr(34) + "customField1" + Chr(34) + ": null", Chr(34) + "customField1" + Chr(34) + ": " + Chr(34) + "Processed" + Chr(34))
                    NewJasonRecord = NewJasonRecord + vbCrLf
                End If
            Next

        Next

        'now send the file back up
        Dim client = New RestSharp.RestClient("https://ssapi.shipstation.com/orders/createorders")
        client.Timeout = -1
        Dim request = New RestRequest(Method.POST)
        request.AddHeader("Host", "ssapi.shipstation.com")
        request.AddHeader("Authorization", " Basic " + EncodedShipStationID)
        request.AddHeader("Content-Type", "application/json")
        request.AddParameter("application/json", jsonArray, ParameterType.RequestBody)
        Dim response As IRestResponse = client.Execute(request)

        MsgBox(response.Content)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros