Link to home
Start Free TrialLog in
Avatar of Rick Becker
Rick BeckerFlag for United States of America

asked on

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

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)

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rick Becker

ASKER

Hi Leakim971,

WOW this is more than I could have hoped for... the jsonutils utility tool is great. You have given me way more than I need to satisfy my current needs. Thanks very much,.. This is great..

Rick
Again thanks this is way more than I could have expected...

Rick
you welcome