Friday, April 26, 2024
HomeGolangExtract particulars from JSON - Code Overview

Extract particulars from JSON – Code Overview


Hiya everybody , im new in Go
bundle important

import (
“encoding/json”
“fmt”
“internet/http”

"github.com/gorilla/websocket"

)

func important() {
dialer := websocket.DefaultDialer
wsSubscriber, _, err := dialer.Dial(“wss://api.blxrbdn.com/ws”, http.Header{“Authorization”: []string{“MzVkNWIzMDgoqTNhNS00MjVjLTswN2EtZTZjOGU5N2Q3Yzk5OjFkZDhjMDZkYjk1NDcwYTQyNWI5ZmYzZTc1NDYxZWFh”}})

if err != nil {
	fmt.Println(err)
	return
}
subRequest := `{"id": 1, "technique": "subscribe", "params": ["newTxs", {"include": ["tx_contents"]}]}`

err = wsSubscriber.WriteMessage(websocket.TextMessage, []byte(subRequest))
if err != nil {
	fmt.Println(err)
	return
}

for {
	_, nextNotification, err := wsSubscriber.ReadMessage()
	if err != nil {
		fmt.Println(err)
       
	}
	fmt.Println(string(nextNotification))
	

}

}

I’ve this output
{“id”:1,“outcome”:“3b6d9090-2757-47b5-95c1-d02a7f753e45”,“jsonrpc”:“2.0”}
{“id”:1,“technique”:“subscribe”,“params”:{“subscription”:“3b6d9090-2757-47b5-95c1-d02a7f753e45”,“outcome”:{“txContents”:{“accessList”:[],“chainId”:“0x1”,“from”:“0x1f922a3854e8cf160b294af56f980f92f7a4409f”,“fuel”:“0x39567”,“hash”:“0xd85de6b1a33744a0c2985945c199a83788d2309594f0f6daf6a4aaec9e9979d8”,“enter”:“0x5ae401dc0000000000000000000000000000000000000000000000000000000063218a1900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e4472b43f300000000000000000000000000000000000000000000000000354a6ba7a180000000000000000000000000000000000000000000000000000000015134c8ea9f00000000000000000000000000000000000000000000000000000000000000800000000000000000000000001f922a3854e8cf160b294af56f980f92f7a4409f0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000008eb18e507ae851740ede9a4ce2e594b05981de6d00000000000000000000000000000000000000000000000000000000”,“maxFeePerGas”:“0x2b02a98dc”,“maxPriorityFeePerGas”:“0x59682f00”,“nonce”:“0xa7”,“r”:“0x699a733ef2c9b4e484d2f47a44b86e27cd249e9ca839dbb43ab7201a17f27656”,“s”:“0x6cba1515c0a284ccbc722c41ecb972ed889175560217ff5970e7d64a27c526c8”,“to”:“0x68b3465833fb72a70ecdf485e0e4c7bd8665fc45”,“kind”:“0x2”,“v”:“0x1”,“worth”:“0x354a6ba7a18000”}}}}

First half about my subscription
Second half is subRequest output
I must output solely particulars from subRequest starting from
“outcome”:{“txContents”:{“accessList”:[],“chainId”:“0x1”,“from”:“0x1f922a3854e8cf160b294af56f980f92f7a4409f”…}}}}

I made it by
myResponse := make(map[string]interface{})
err2 := json.Unmarshal([]byte(nextNotification), &myResponse)
if err2 != nil {
fmt.Println(err2)
}

	txC := myResponse["params"]

	jsonTx, _ := json.Marshal(txC)

	fmt.Println(string(jsonTx))
}

And output is :
null
{“outcome”:{“txContents”:{“from”:“0xcac76b259ad18a269be1987c0e8a07a2b855f7b6”,“fuel”:“0xc350”,“gasPrice”:“0x1e7a775d0”,“hash”:“0xc21e74b18a09d0dffe0ebd581317882bcc5479448575ba44ffd4e1ef611b998d”,“enter”:“0x”,“nonce”:“0x6d0a”,“r”:“0xac50fe84bf5f31c11142dc349553ce1c0ba23ef10b51936032710fed8c15bf9f”,“s”:“0x4e250ae37e452cd71386aa6c445b69076e26c514719bbab4768f1d100fbf086f”,“to”:“0x12396dcdb6d82adb7126707f191ebcfb7d9803fb”,“kind”:“0x0”,“v”:“0x26”,“worth”:“0x18de76816d8000”}},“subscription”:“a20fe909-998b-4f7d-82ac-2f7a3c7e45a7”}

Learn how to delete null from output and begin the output from
{“txContents:{“from”:“0xcac76b259ad18a269be1987c0e8a07a2b855f7b6”,“fuel”:“0xc350”,“gasPrice”:“0x1e7a775d0”,“hash”:“0xc21e74b18a09d0dffe0ebd581317882bcc5479448575ba44ffd4e1ef611b998d”,“enter”:“0x”,“nonce”:“0x6d0a”,“r”:“0xac50fe84bf5f31c11142dc349553ce1c0ba23ef10b51936032710fed8c15bf9f”,“s”:“0x4e250ae37e452cd71386aa6c445b69076e26c514719bbab4768f1d100fbf086f”,“to”:“0x12396dcdb6d82adb7126707f191ebcfb7d9803fb”,“kind”:“0x0”,“v”:“0x26”,“worth”:“0x18de76816d8000”}},“subscription”:“a20fe909-998b-4f7d-82ac-2f7a3c7e45a7”}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments