无网络连接
  1. Home
  2. Questions

(re)formatting a Json array

作者:isedwinr @isedwinr
    2022-12-05 16:55:36.019Z

    Hello,

    I'm trying to insert an entire csv file into a google spreadsheet via the google API and I'm not sure how to get the correct (array) format

    The Google API needs this format for an array:
    "values": [
    ["Data", "Name","Mark"],
    ["blabla", "John","A7"]
    ],

    but when I transform my csv file to a JSON array within TA I get a different array format

    [{
    "Data": "blabla",
    "Name": "John",
    "Mark": "A7"
    }]

    which the Google API refuses to process ;)

    Any ideas?

    Edwin

    • 4 回复
    1. D
      Daniel Horton @daniel.horton
        2023-01-04 10:44:14.961Z

        Hi Edwin,

        After checking the Google API's JSON you have provided above it doesn't appear to be valid JSON - https://jsonlint.com/

        Google's JSON

        ThinkAutomation's JSON

        If the format needs to be as Google are requesting then you would likely need to compose the JSON manually or scripted within a Set Variable action type.

        1. I
          isedwinr @isedwinr
            2023-01-04 11:00:37.780Z

            Hello and Thanks Daniel,

            I'm sorry I made a mistake and I see I pasted an extra comma with the array, it had to be his

            [
            ["Data", "Name","Mark"],
            ["blabla", "John","A7"]
            ]

            See this how Google states it: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values

            1. D
              Daniel Horton @daniel.horton
                2023-01-04 14:39:53.343Z2023-01-04 14:52:16.542Z

                Can you provide screen shots of how you are attempting to achieve this within ThinkAutomation so that we can review it please?

                If you are using the Set Variable's Opertation of Convert CSV to JSON then it will always produce something like this from a CSV file as this is the valid manner to build CSV data from a spreadsheet into an array of objects. You'd likely need to script something to match Google's requirement of a line for the head and then a line for each row. It's not really something which we'd be able to support in our action types as it isn't what you'd expect to achieve with the data when converting a CSV into a JSON array.

                This is a sample I made with multiple rows using what I believe to have been your approach and this is valid JSON but not for the object that Google are expecting in their API. The only route for this would be scripting using a custom written behaviour:

                [
                  {
                    "Name": "DH",
                    "Job": "TS"
                  },
                  {
                    "Name": "LD",
                    "Job": "TS"
                  },
                  {
                    "Name": "KG",
                    "Job": "TS"
                  },
                  {
                    "Name": "OA",
                    "Job": "TS"
                  },
                  {
                    "Name": "TP",
                    "Job": "DV"
                  }
                ]
                
                1. Iisedwinr @isedwinr
                    2023-01-05 07:30:32.279Z

                    Hello Daniel,

                    Ok thanks, this is the way I'm doing it now (it's with some test JSON data)

                    {
                    "range": "A1:Z",
                    "majorDimension": "ROWS",
                    "values":[
                    %multilines%
                    ]
                    }

                    It works, but TA takes a very long time to transpose/convert the entire JSON (10K+ records)

                    As you may notice, I'm not a programmer and I don't have much experience with APIs, JSON arrays/objects, etc, so maybe it's better to let you guys build it as a custom automation.

                    Edwin