Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi guys i have a code this code print this
https://api.binance.com/api/v3/ticker?symbols=["BTCUSDT","BNBBTC",]&windowSize=1d&type=full
i want remove the last comma inside a square parentesis i want somthing like this
https://api.binance.com/api/v3/ticker?symbols=["BTCUSDT","BNBBTC"]&windowSize=1d&type=full
how is possible do that

What I have tried:

<pre>urlencode() {
    local string="$1"
    local length="${#string}"
    local result=""

    for (( i = 0; i < length; i++ )); do
        local c="${string:$i:1}"
        case $c in
            [-_.~a-zA-Z0-9]) result+="$c" ;;
            *) result+="%$(printf '%02X' "'$c")" ;;
        esac
    done

    echo "$result"
}


# ----------------  BINANCE  -------------------------------------
# Market
# Market Data

ServerBinance="https://api.binance.com/api/v3"
response="" 
_Bin_RollingWindowPriceChangeStat() {
    local apiUrl="$ServerBinance/ticker"

    # Verifica la presenza di symbol o symbols
    if [[ "$1" == *","* ]]; then
        IFS=',' read -ra symbols <<< "$1"
        local encodedSymbols=$(IFS=,; printf "\"%s\"," "${symbols[@]}")
        apiUrl+="?symbols=[$encodedSymbols]"
        shift
    elif [ -n "$1" ]; then
        apiUrl+="?symbol=[\"$1\"]"
        shift
    fi

    # Aggiungi i restanti parametri
    if [ -n "$1" ] && [ -n "$2" ]; then
        apiUrl+="&windowSize=$1&type=$2"
    fi

    # Visualizza l'URL costruito prima della chiamata di curl
    echo "URL: $apiUrl"

    response=$(curl -s -X GET "$apiUrl")
}
Posted
Updated 18-Feb-24 19:26pm

1 solution

Use string replace: How to Replace Substring in Bash Natively[^]
The string to search for: ",]
The string to replace it with: "]

If the "spare comma" is there, it'll be removed. If it isn't, the string will be unchanged.
 
Share this answer
 

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