Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So far my bash script is as follows:

Bash
#!/bin/sh
sessionnumber=0
curl --data '{"command":"listsessions","transaction":"123","adminsecret":"secret"}' -H "Content-Type: application/json" http://127.0.0.1:8088/admin


This successfully returns the following:

{
   "result": "success",
   "transaction": "123",
   "sessions": []
}


I am trying to create a for loop in the .sh script that iterates over each of the elements in the sessions array, and increments the value of sessionnumber by 1 but I am having difficulty parsing the json array that is output from the curl command.

What I have tried:

I have tried a number of things but all of my problems seem to relate to unable to parse the array that is 'sessions' which is inside the returned json array
Posted
Updated 6-Jan-22 4:27am
v2

1 solution

Parsing JSON using standard tools (grep, awk, sed, etc), is probably more difficult than you imagine. You really need a JSON parser. This is discussed here: bash - Parsing JSON with Unix tools - Stack Overflow[^]
Looking at the home page for jq Parsing JSON with jq[^] it describes itself a "sed for JSON", so it might be a good way to go.

I have not tried this, nor do I know of its suitability for purpose, so Caveat Usor.
 
Share this answer
 
Comments
mc96 11-Jan-22 10:13am    
jq solved my problem!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900