Jump to content

Flip JSON array


Go to solution Solved by TheXman,

Recommended Posts

Hello all,

I have such JSON file (the example is simplified for demonstration, the original one can content about 20000 array blocks or even more). 
I need the audience help to flip the "Flip_Me" array.

{ 
    "Example": {
    "EnableList": true, 
    "Color": true
        "Flip_Me": [
            {
                "Name": "Test_1",
                "Type": {
                    "CAT": 1.1,
                    "DOG": 1.2,
                    "FOX": 1.3
                },
                "Form": {
                    "ABC": 1.4,
                    "CDE": 1.5,
                    "NON": 1.6
                }
            },
            {
                "Name": "Test_2",
                "Type": {
                    "CAT": 2.1,
                    "DOG": 2.2,
                    "FOX": 2.3
                },
                "Form": {
                    "ABC": 2.4,
                    "CDE": 2.5,
                    "NON": 2.6
                }
            },
            {
                "Name": "Test_3",
                "Type": {
                    "CAT": 3.1,
                    "DOG": 3.2,
                    "FOX": 3.3
                },
                "Form": {
                    "ABC": 3.4,
                    "CDE": 3.5,
                    "NON": 3.6
                }
            }
        ]
    }
}

 

Thanks

Edited by RAMzor
Link to comment
Share on other sites

  • Solution
Posted (edited)

Your JSON has an error.  There should be a comma after

"Color": true

 

I'm not sure what you mean by "flip" the array.  If you mean reverse the entries in the array, then using jq you can do something like this:

#include <Constants.au3>
#include <jq\jq.au3>


jq_example()

Func jq_example()
    Const $JSON_DATA = '{"Example":{"EnableList":true,"Color":true,"Flip_Me":[{"Name":"Test_1","Type":{"CAT":1.1,"DOG":1.2,"FOX":1.3},"Form":{"ABC":1.4,"CDE":1.5,"NON":1.6}},{"Name":"Test_2","Type":{"CAT":2.1,"DOG":2.2,"FOX":2.3},"Form":{"ABC":2.4,"CDE":2.5,"NON":2.6}},{"Name":"Test_3","Type":{"CAT":3.1,"DOG":3.2,"FOX":3.3},"Form":{"ABC":3.4,"CDE":3.5,"NON":3.6}}]}}'

    Local $sResult
    Local $hTimer = TimerInit()


    ;Identify path to exe
    _jqInit("C:\Utils\JQ\jq-win64.exe")
    If @error Then Exit MsgBox($MB_ICONERROR + $MB_TOPMOST, "jqInit() Error", "Unable to find jq executable - @error = " & @error)

    ;Process JSON dataset
    $sResult = _jqExec($JSON_DATA, ".Example.Flip_Me = (.Example.Flip_Me|reverse)")

    ;Display result
    ConsoleWrite(StringFormat('Time to get result: %.3f seconds', TimerDiff($hTimer)/1000) & @CRLF)
    ConsoleWrite($sResult & @CRLF)
EndFunc

My result:

Time to get result: 0.023 seconds
{
  "Example": {
    "EnableList": true,
    "Color": true,
    "Flip_Me": [
      {
        "Name": "Test_3",
        "Type": {
          "CAT": 3.1,
          "DOG": 3.2,
          "FOX": 3.3
        },
        "Form": {
          "ABC": 3.4,
          "CDE": 3.5,
          "NON": 3.6
        }
      },
      {
        "Name": "Test_2",
        "Type": {
          "CAT": 2.1,
          "DOG": 2.2,
          "FOX": 2.3
        },
        "Form": {
          "ABC": 2.4,
          "CDE": 2.5,
          "NON": 2.6
        }
      },
      {
        "Name": "Test_1",
        "Type": {
          "CAT": 1.1,
          "DOG": 1.2,
          "FOX": 1.3
        },
        "Form": {
          "ABC": 1.4,
          "CDE": 1.5,
          "NON": 1.6
        }
      }
    ]
  }
}

In jqPlayground:

image.png.288c023f18ed25ff0ed209e879408703.png

 

Edited by TheXman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...