Jump to content

Help, parse JSON array to a autoit array


Kyan
 Share

Go to solution Solved by llewxam,

Recommended Posts

Hi, I'm trying to 'convert' a JSON's array to autoit arrays

Found this UDF: www.autoitscript.com/forum/topic/104150-json-udf-library-fully-rfc4627-compliant/

But doesn't work with this kind of arrays: [{"z1":"vanueh","on":true}]

Works for: {"error":"..."}

Script:

#include "JSON.au3"
#include "array.au3"
#noTrayIcon
$s = ' [{"z1":"vanueh","on":true}]'
$s2='{"error":"Error MSG"}'
$t = _JSONDecode($s)
$t2 = _JSONDecode($s2)
_ArrayDisplay($t)
_ArrayDisplay($t2)

Should it be done using a regex?

Thank you for your time

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

  • Solution

I see you still haven't had any suggstions here, I did some tinkering and here is what I came up with:

#include "JSON.au3"
#include "array.au3"
#NoTrayIcon

$s = ' [{"z1":"vanueh","on":true}]'
$s2 = '{"error":"Error MSG"}'

Parse($s)
Parse($s2)
Exit


Func Parse($sTemp)
    If StringInStr($sTemp, ",") Then
        $sTemp = StringRegExpReplace($sTemp, "[\[\]{}]", "")
        $sBreak = StringSplit($sTemp, ",")
        For $a = 1 To $sBreak[0]
            $t = _JSONDecode("{" & $sBreak[$a] & "}")
            _ArrayDisplay($t, "multi " & $a & " of " & $sBreak[0])
        Next
    Else
        $t = _JSONDecode($sTemp)
        _ArrayDisplay($t, "single")
    EndIf
EndFunc   ;==>Parse

It seemed to me the easiest way to deal with the issue.  When the result is nested, strip all brackets, split by comma, then add brackets back to each element of the resulting array.  Hopefully this offers you some way to move forward with your script.

Ian

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

wow, works :D, took a bit of time to check what you done and why a json array line was appearing on the output.

To process arrays with more than one line do you have some suggestion? (no structs here :)

Thank you, was stuck in this part :)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

LOL, to be honest I know NOTHING about JSON, so I am stabbing in the dark here.

When you say "To process arrays with more than one line", what exactly do you mean?  If you mean the string (JSON array) could have more than 1 comma it is already fixed, it will work as-is for thousands of nested elements.  If you mean you may have more than one JSON array that you want to parse at the same time, you could just jam them together like "$sNew = $s & "," & s2" and send $sNew to the Parse function, since StringRegExpReplace will remove all brackets and make one giant AutoIt array of the results.  Just make sure you add the comma in between each JSON array as I showed or you will have a bit of funkiness where they meet up.....

If I missed the point completely let me know.

Ian
 

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

For example: 

[{"z1":"vanueh","on":true},{"z1":"vanueh","on":true}]

It is going to be in the same array two different "lines", but I guess only with structs it will get organized

Nevermind :)

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Link to comment
Share on other sites

Unless I'm missing the point of JSON I don't see any problems here.  And running what you posted above it worked just fine  :)

Ian

My projects:

  • IP Scanner - Multi-threaded ping tool to scan your available networks for used and available IP addresses, shows ping times, resolves IPs in to host names, and allows individual IPs to be pinged.
  • INFSniff - Great technicians tool - a tool which scans DriverPacks archives for INF files and parses out the HWIDs to a database file, and rapidly scans the local machine's HWIDs, searches the database for matches, and installs them.
  • PPK3 (Persistent Process Killer V3) - Another for the techs - suppress running processes that you need to keep away, helpful when fighting spyware/viruses.
  • Sync Tool - Folder sync tool with lots of real time information and several checking methods.
  • USMT Front End - Front End for Microsoft's User State Migration Tool, including all files needed for USMT 3.01 and 4.01, 32 bit and 64 bit versions.
  • Audit Tool - Computer audit tool to gather vital hardware, Windows, and Office information for IT managers and field techs. Capabilities include creating a customized site agent.
  • CSV Viewer - Displays CSV files with automatic column sizing and font selection. Lines can also be copied to the clipboard for data extraction.
  • MyDirStat - Lists number and size of files on a drive or specified path, allows for deletion within the app.
  • 2048 Game - My version of 2048, fun tile game.
  • Juice Lab - Ecigarette liquid making calculator.
  • Data Protector - Secure notes to save sensitive information.
  • VHD Footer - Add a footer to a forensic hard drive image to allow it to be mounted or used as a virtual machine hard drive.
  • Find in File - Searches files containing a specified phrase.
Link to comment
Share on other sites

  • 2 weeks later...

Nailed it :D

Func Parse($sTemp)
    Local $aArray[1], $n=1
    ;$sTemp = StringRegExpReplace($sTemp, "[\[\]]", "")
    ;$sBreak = StringSplit($sTemp,",")
    $sBreak = StringRegExp($sTemp,'({.+?})',3)
    If Not @error Then
        For $a = 0 To UBound($sBreak)-1
            $js = _JSONDecode($sBreak[$a])
            $cn = UBound($js)
            If $cn > $n Then $n = $cn
            ReDim $aArray[$n][$a*2+2]
            For $i = 0 to $cn - 1
                $aArray[$i][$a*2] = $js[$i][0]
                $aArray[$i][$a*2+1] = $js[$i][1]
            Next
        Next
        Return $aArray
    EndIf
EndFunc   ;==>Parse

Works for Json arrays, like:  [{"z1":"vanueh","on":true},{"z1":"vanueh","on":true}], and they don't need to be the same size (rows)

or for simple ones,  {"z1":"vanueh","on":true}

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...