Jump to content

How to get number of sections in JSON file


ur
 Share

Recommended Posts

I have a github repository and when I run the below command it will retrieve a json file.

curl --user "user:password" https://api.github.com/repos/ukreddy-erwin/Build-war/commits > Desktop/uday.html

The output json file will be of the below format.

[
  {
    "sha": "525ddee5f25628f89c04980d7f683e9cd8f56b7a",
    "commit": {
      "author": {
        "name": "ukreddy",
        "email": "ukreddy@erwin.com",
        "date": "2016-12-26T12:27:14Z"
      },
      "committer": {
        "name": "ukreddy",
        "email": "ukreddy@erwin.com",
        "date": "2016-12-26T12:27:14Z"
      },
      "message": "changed gwt and gxt folder structure to build.xml",
      "tree": {
        "sha": "8d4f2cbca93efec18be8a17f25f78ad168e65347",
        "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/git/trees/8d4f2cbca93efec18be8a17f25f78ad168e65347"
      },
      "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/git/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a",
      "comment_count": 0
    },
    "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a",
    "html_url": "https://github.com/ukreddy-erwin/Build-war/commit/525ddee5f25628f89c04980d7f683e9cd8f56b7a",
    "comments_url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/525ddee5f25628f89c04980d7f683e9cd8f56b7a/comments",
    "author": {
      "login": "ukreddy-erwin",
      "id": 21240709,
      "avatar_url": "https://avatars.githubusercontent.com/u/21240709?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/ukreddy-erwin",
      "html_url": "https://github.com/ukreddy-erwin",
      "followers_url": "https://api.github.com/users/ukreddy-erwin/followers",
      "following_url": "https://api.github.com/users/ukreddy-erwin/following{/other_user}",
      "gists_url": "https://api.github.com/users/ukreddy-erwin/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/ukreddy-erwin/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/ukreddy-erwin/subscriptions",
      "organizations_url": "https://api.github.com/users/ukreddy-erwin/orgs",
      "repos_url": "https://api.github.com/users/ukreddy-erwin/repos",
      "events_url": "https://api.github.com/users/ukreddy-erwin/events{/privacy}",
      "received_events_url": "https://api.github.com/users/ukreddy-erwin/received_events",
      "type": "User",
      "site_admin": false
    },
    "committer": {
      "login": "ukreddy-erwin",
      "id": 21240709,
      "avatar_url": "https://avatars.githubusercontent.com/u/21240709?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/ukreddy-erwin",
      "html_url": "https://github.com/ukreddy-erwin",
      "followers_url": "https://api.github.com/users/ukreddy-erwin/followers",
      "following_url": "https://api.github.com/users/ukreddy-erwin/following{/other_user}",
      "gists_url": "https://api.github.com/users/ukreddy-erwin/gists{/gist_id}",
      "starred_url": "https://api.github.com/users/ukreddy-erwin/starred{/owner}{/repo}",
      "subscriptions_url": "https://api.github.com/users/ukreddy-erwin/subscriptions",
      "organizations_url": "https://api.github.com/users/ukreddy-erwin/orgs",
      "repos_url": "https://api.github.com/users/ukreddy-erwin/repos",
      "events_url": "https://api.github.com/users/ukreddy-erwin/events{/privacy}",
      "received_events_url": "https://api.github.com/users/ukreddy-erwin/received_events",
      "type": "User",
      "site_admin": false
    },
    "parents": [
      {
        "sha": "db4d61c5bbef555e6cc9f2e852aaf9d75772475c",
        "url": "https://api.github.com/repos/ukreddy-erwin/Build-war/commits/db4d61c5bbef555e6cc9f2e852aaf9d75772475c",
        "html_url": "https://github.com/ukreddy-erwin/Build-war/commit/db4d61c5bbef555e6cc9f2e852aaf9d75772475c"
      }
    ]
  }
]

So, I need to parse above file and get the number of opening and closing brackets {} .

So that would be the number of commits in the repository.

Any suggestion on how to parse this file of json format.

Link to comment
Share on other sites

Quote

get the number of opening and closing brackets {}

StringInStr can find those, and retrieves the position.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

_Example()
Func _Example()
    Local $sJSON = ClipGet()
    StringReplace($sJSON,'{','')
    ConsoleWrite("! { count=" & @extended & @CRLF)
    StringReplace($sJSON,'}','')
    ConsoleWrite("! } count=" & @extended & @CRLF)
EndFunc

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

May be this is wrong now.
But I got below input from support team that.

 

WHen we run the curl command differently as below.

we can get the total count of commits by looking at the Link header when making a request with only 1 commit per page.
For example, here's a request to the Linguist repository:
curl -I https://api.github.com/repos/github/linguist/commits?per_page=1.


Looking at our Link header, you can see the last page value:
Link: <https://api.github.com/repositories/1725199/commits?per_page=1&page=2>; rel="next", <https://api.github.com/repositories/1725199/commits?per_page=1&page=5454>; rel="last"
Since the last page is 5454, this means that there are a total of 5454 commits in that repository
 

How to get this number by parsing the output.

Link to comment
Share on other sites

This is the command we can run for example.

curl -I https://api.github.com/repos/github/linguist/commits?per_page=1

And the command output is as below.

HTTP/1.1 200 OK
Server: GitHub.com
Date: Thu, 29 Dec 2016 10:17:35 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 3448
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 57
X-RateLimit-Reset: 1483009802
Cache-Control: public, max-age=60, s-maxage=60
Vary: Accept
ETag: "9120c8c80a4342a0b46cad7086358638"
Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT
X-GitHub-Media-Type: github.v3; format=json
Link: <https://api.github.com/repositories/1725199/commits?per_page=1&page=2>; rel="next", <https://api.github.com/repositories/1725199/commits?per_page=1&page=5454>; rel="last"
Access-Control-Expose-Headers: ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval
Access-Control-Allow-Origin: *
Content-Security-Policy: default-src 'none'
Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
X-Content-Type-Options: nosniff
X-Frame-Options: deny
X-XSS-Protection: 1; mode=block
Vary: Accept-Encoding
X-Served-By: edf23fdc48375d9066b698b8d98062e9
X-GitHub-Request-Id: 673CD4B2:772D:DC0C69D:5864E2BF

So, if we observe the Link: line above, the number 5454 is visible, how to read this number from the output.?

 

Can anyone suggest that.

Link to comment
Share on other sites

If it's only that output maybe you can do a regex that uses that '&page=' '>;' and get what's in the middle.

Spoiler

Renamer - Rename files and folders, remove portions of text from the filename etc.

GPO Tool - Export/Import Group policy settings.

MirrorDir - Synchronize/Backup/Mirror Folders

BeatsPlayer - Music player.

Params Tool - Right click an exe to see it's parameters or execute them.

String Trigger - Triggers pasting text or applications or internet links on specific strings.

Inconspicuous - Hide files in plain sight, not fully encrypted.

Regedit Control - Registry browsing history, quickly jump into any saved key.

Time4Shutdown - Write the time for shutdown in minutes.

Power Profiles Tool - Set a profile as active, delete, duplicate, export and import.

Finished Task Shutdown - Shuts down pc when specified window/Wndl/process closes.

NetworkSpeedShutdown - Shuts down pc if download speed goes under "X" Kb/s.

IUIAutomation - Topic with framework and examples

Au3Record.exe

Link to comment
Share on other sites

3 hours ago, ur said:

May be this is wrong now.
But I got below input from support team that.

 

WHen we run the curl command differently as below.

we can get the total count of commits by looking at the Link header when making a request with only 1 commit per page.
For example, here's a request to the Linguist repository:
curl -I https://api.github.com/repos/github/linguist/commits?per_page=1.


Looking at our Link header, you can see the last page value:
Link: <https://api.github.com/repositories/1725199/commits?per_page=1&page=2>; rel="next", <https://api.github.com/repositories/1725199/commits?per_page=1&page=5454>; rel="last"
Since the last page is 5454, this means that there are a total of 5454 commits in that repository
 

How to get this number by parsing the output.

This look like in TeamViewer API (look in my signature).
When github have to many information to send to you, then github is splitting this data into pages.
You should get each page and parse them in a loop.

btw.
I think you should try to use WinHTTP instead CURL, but for now I want to ask how you use CURL with AutoIt ?

 

Edited by mLipok

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

Spoiler

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST APIErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 *

 

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * 

OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskSchedulerIE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related:How to get reference to PDF object embeded in IE * IE on Windows 11

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

I also encourage you to check awesome @trancexx code:  * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuffOnHungApp handlerAvoid "AutoIt Error" message box in unknown errors  * HTML editor

winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming"
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

Signature last update: 2023-04-24

Link to comment
Share on other sites

I'm guessing you're using CURL because you have either Cygwin or are using Windows 10's Subsystem for Linux. Here's a quick solution using that...

curl --silent -I https://api.github.com/repos/github/linguist/commits?per_page=1 | grep "^Link" | sed -e 's/.*page=\([0-9]*\)>; rel=\"last.*/\1/'

 

Edited by JohnQSmith
Trying to remove syntax highlighting

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

Here it is using WinHTTP as suggested by @mLipok

$oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
$oHTTP.Open("GET", "http://api.github.com/repos/github/linguist/commits?per_page=1")
$oHTTP.Send()
$header = $oHTTP.Getresponseheader("Link")
$numCommits = StringRegExpReplace( $header, '.*page=([0-9]+)>; rel="las.*', '$1' )
ConsoleWrite($numCommits)

 

Whenever someone says "pls" because it's shorter than "please", I say "no" because it's shorter than "yes".

Link to comment
Share on other sites

This is my solution for this....using curl command

;#include "ProcessEx.au3"
#include "ProcessEx_AddedNewEntryForLogging.au3"
#include <Array.au3>
#include <WinAPIFiles.au3>

Func NoChangesRequired($sBuildCommand = 'curl -I --user "userid:password" https://api.github.com/repos/erwin-inc/erwin-main/commits?per_page=1',$commitfile = @TempDir&"\git_erwin_commit.txt")

$valueRead = ProcessGitCommitCount($sBuildCommand)

If not FileExists($commitfile) Then
    FileWrite($commitfile,$valueRead)
    ;MsgBox(0,"","Changes are there.")
    return False
ELse
     Local $hFileOpen = FileOpen($commitfile, $FO_READ)
     If $hFileOpen = -1 Then
        ;MsgBox($MB_SYSTEMMODAL, "", "An error occurred when reading the file.")
        Return False
     EndIf

     ; Read the fist line of the file using the handle returned by FileOpen.
    Local $sFileRead = FileReadLine($hFileOpen, 1)

    ; Close the handle returned by FileOpen.
    FileClose($hFileOpen)
    If Number(StringStripWS($sFileRead,$STR_STRIPLEADING + $STR_STRIPTRAILING))= Number($valueRead) Then
        ;MsgBox(0,"","No change in commit")
        Return True
    Else
        ;MsgBox(0,"","Changes are there")
        FileDelete($commitfile)
        FileWrite($commitfile,$valueRead)
        Return False
    EndIf

EndIf

EndFunc

Func ProcessGitCommitCount($sBuildCommand)
$sOutput = _Process_RunCommand($PROCESS_RUNWAIT, $PROCESS_COMMAND & """" &$sBuildCommand &"""") ; Record the output
$Ret = @extended ; Note the exit code
;MsgBox(0,$Ret,$sOutput)
Local $strTemp = StringSplit($sOutput, @CRLF,$STR_ENTIRESPLIT)
;_ArrayDisplay($strTemp)


$u = ""
For $vElement In $strTemp
        ;$sString = $sString & $vElement & @CRLF

        If StringInStr($vElement,"Link:") then
            $u = $vElement
            ExitLoop
        EndIf

Next

$u = StringStripWS($u,$STR_STRIPLEADING + $STR_STRIPTRAILING)
If StringLen($u)=0 Then
    ;MsgBox(0,"","No output from GitHub")
    return "No output"
ElseIf StringInStr($u,"Link:") Then
    return ProcessHeader($u)
Else
    ;MsgBox(0,"","Some random unnecessary output")
    return "Some random unnecessary output"
EndIf
EndFunc

;to remove any empty strings in array and reduce its size
Func RemoveEmptyArrayValues(ByRef $arrLines)
  $intCount = 1
  While $intCount < UBound($arrLines)
         $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING)
         If StringLen($arrLines[$intCount])=0 Then
            _ArrayDelete($arrLines, $intCount)
            $intCount = $intCount - 1
         EndIf
         $intCount = $intCount + 1
   WEnd
EndFunc

Func RemoveOtherData(ByRef $arrLines)
  $intCount = 1
  While $intCount < UBound($arrLines)
         $arrLines[$intCount] = StringStripWS($arrLines[$intCount],$STR_STRIPLEADING + $STR_STRIPTRAILING)
         If StringInStr($arrLines[$intCount],">;") Then
             $arrLines[$intCount] = StringTrimRight($arrLines[$intCount], 2)
         Else
            _ArrayDelete($arrLines, $intCount)
            $intCount = $intCount - 1
         EndIf
         $intCount = $intCount + 1
   WEnd
EndFunc

Func ProcessHeader($header)
     $splittedArray = StringSplit ( $header, "per_page=1&page=" )
     ;_ArrayDisplay($splittedArray)
     RemoveEmptyArrayValues($splittedArray)
     ;_ArrayDisplay($splittedArray)
     RemoveOtherData($splittedArray)
     ;_ArrayDisplay($splittedArray)
     ;MsgBox(0,"",$splittedArray[UBound($splittedArray)-1])
     return $splittedArray[UBound($splittedArray)-1]
EndFunc

 

Edited by ur
Link to comment
Share on other sites

On 12/29/2016 at 7:37 PM, mLipok said:

This look like in TeamViewer API (look in my signature).
When github have to many information to send to you, then github is splitting this data into pages.
You should get each page and parse them in a loop.

btw.
I think you should try to use WinHTTP instead CURL, but for now I want to ask how you use CURL with AutoIt ?

 

Can you help me how to use WinHTTP so that I can use them in later scripts.

And I used CURL by the UDF mentioned above

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...