Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"unknown time format" error while unmarshalling kite order object #111

Closed
debslab opened this issue Feb 9, 2025 · 4 comments
Closed

"unknown time format" error while unmarshalling kite order object #111

debslab opened this issue Feb 9, 2025 · 4 comments

Comments

@debslab
Copy link

debslab commented Feb 9, 2025

Use case: Save kite orders in redis and get it back.
Issue: While retrieving back from redis, the unmarshal of JSON fails with error as "unknown time format"
Root cause: If the order is rejected due to insufficient margin, we get exchange_update_timestamp and exchange_timestamp as null which throws error in the custom marshal written in models.Time package. Following is a sample response which caused the unmarshal error.

{ "status_message": "Insufficient funds. xxxx", "status_message_raw": "RMS:Margin Exceeds,Required:xxxx", "order_timestamp": "2025-02-07 10:28:51", "exchange_update_timestamp": null, "exchange_timestamp": null, "variety": "regular", "modified": false, "exchange": "BFO", "tradingsymbol": "SENSEX2521177800CE", "instrument_token": 215541253, "order_type": "LIMIT", "transaction_type": "SELL", "validity": "DAY", "validity_ttl": 0, "product": "NRML", "quantity": 940, "disclosed_quantity": 0, "price": 417, "trigger_price": 0, "average_price": 0, "filled_quantity": 0, "pending_quantity": 0, "cancelled_quantity": 0, "market_protection": 0, "meta": {}, "tag": "D8VxR", "tags": ["D8VxR"], "guid": "31654X5Rml1rmZcqK1" },

Please check and let me know if you need further details.

@Surya-7890
Copy link

can you please provide the code where you called the UnmarshalJSON function?

@debslab
Copy link
Author

debslab commented Mar 2, 2025

After receiving json from redis, I use below code to unmarshall where I am getting "unknown time format" error.

json.Unmarshal([]byte(jsonData), &result)

@Surya-7890
Copy link

I tried it out myself using redis and there was no error, the code i wrote is below:

var (
	ctx = context.Background()
	RDB *redis.Client
)

func init() {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "localhost:6379",
		Password: "", // no password set
		DB:       0,  // use default DB
	})
	RDB = rdb

	data, err := os.ReadFile("./data.json")
	if err != nil {
		panic("error reading file: " + err.Error())
	}

	if err := rdb.Set(ctx, "key", data, 0).Err(); err != nil {
		panic("error while setting: " + err.Error())
	}
}

func main() {

	res, err := RDB.Get(ctx, "key").Result()
	if err != nil {
		panic("error while getting: " + err.Error())
	}

	result := kiteconnect.Order{}
	err = json.Unmarshal([]byte(res), &result)
	if err != nil {
		panic(err.Error())
	}

	fmt.Println(result.OrderTimestamp)
}

The data.json file contains the sample response provided.

The above code produced the following output:

go run main.go 
2025-02-07 10:28:51 +0530 IST

I think the issue is with the type of the result variable in your code. Can you please check it and let me know if the issue persists.

@debslab
Copy link
Author

debslab commented Mar 3, 2025

Thanks, probably it's happening due to use of generics in my code. Anyways, I did a workaround in my code to avoid null i.e. if the timestamp is null, I am adding current time before saving to redis. Below is my code which I use to get an object from redis. I had to use generics so that I can retrieve any object passing it's type.

`func GetJSON[T any](key string) (T, error) {
var result T
jsonData, err := redisClient.client.Get(redisClient.ctx, key).Result()
if err != nil {
return result, err
}

if err := json.Unmarshal([]byte(jsonData), &result); err != nil {
	return result, fmt.Errorf("could not unmarshal JSON: %w", err)
}

return result, nil

}`

@debslab debslab closed this as completed Mar 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants