Skip to content

Change example_crates's output format to be JSON #1932

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

Open
nymius opened this issue Mar 28, 2025 · 7 comments
Open

Change example_crates's output format to be JSON #1932

nymius opened this issue Mar 28, 2025 · 7 comments

Comments

@nymius
Copy link
Contributor

nymius commented Mar 28, 2025

Describe the enhancement
Change all the example_crates output format to be JSON.

Use case
Easier output information extraction through jq tool, for better integration with shell script or other external cli tools.

Additional context
I have been using heavily the example_crates to test some workflows, mainly written as shell scripts or similar.
The problem is I find more intuitive to extract information from JSON using jq than use regex to extract the same information from strings.
There are any concerns about changing all the example_crates output format to be in JSON?
Is this change behind the scope of the example_crates or it is a legit enhancement?
May this break the development workflow of anyone testing things with example_crates too?

@sudo-rsingh
Copy link

@nymius hey can you pls assign this issue to me?

@nymius
Copy link
Contributor Author

nymius commented Mar 31, 2025

Hello @sudo-rsingh, notice here I was seeking for feedback about the justifiability of the change. The project can decide this is not necessary or it breaks some workflow, and not merge it at the end, but I think is something trivial, so go ahead and start working on it if you want.

@SharanRP
Copy link

Hello @nymius
I have submitted PR #1920, which implements the changes to enable this feature on example_wallet_rpc. Could you please review the modifications ?

@notmandatory notmandatory moved this to Discussion in BDK Chain Mar 31, 2025
@notmandatory notmandatory moved this to Discussion in BDK Wallet Mar 31, 2025
@luisschwab
Copy link
Contributor

Agree, this would be useful.

@LLFourn
Copy link
Contributor

LLFourn commented Apr 3, 2025

I think this would be a good idea. I'd start by structuring the output for each command into types. Then have a -j flag that outputs everything in json. If -j isn't specified it should just be human friendly.

There are a few example crates that stream output at they go I suppose they can be changed to make all that go to stderr.

@notmandatory notmandatory transferred this issue from bitcoindevkit/bdk Apr 7, 2025
@notmandatory notmandatory transferred this issue from bitcoindevkit/bdk Apr 7, 2025
@notmandatory notmandatory removed this from BDK Wallet Apr 7, 2025
@notmandatory notmandatory transferred this issue from bitcoindevkit/bdk_wallet Apr 7, 2025
@PranavKumar20
Copy link

Hi everyone 👋,

I’ve implemented a partial solution for this issue and wanted to confirm if I’m on the right track before proceeding further.

I’ve used clap to add a --json (or -j) flag for enabling JSON output, and serde to serialize the values into JSON.

  • When run without any options, the command behaves normally and produces the standard output:
Next unused address: (7) bcrt1qf8ren4rqzpk340lj44whyh9p5uxt3r92wzhkcp
Wallet balance before syncing: 8749.99998731 BTC
Syncing...
Scanning keychain [External] 0   1   2   3   4   5   6   7   8   9   10  11  12  13  14 
Scanning keychain [Internal] 0   1   2   3   4   5   6   7   8   9   10  11  12  13  14 
Wallet balance after syncing: 8749.99998590 BTC
Tx broadcasted! Txid: cb8cef2beb1e724ed9137aea456feb550d36529cfd836a5b08dc4238354c14a3
  • When run with -j or --json, it returns the same information in structured JSON format:
{
   "next_unused_address":{
      "index":6,
      "address":"bcrt1qusvzrqjg4e570whysy20auytqun974w535jkxj"
   },
   "wallet_balance":{
      "before_syncing":8749.99998872,
      "after_syncing":8749.99998731
   },
   "syncing":{
      "external_keychain_scanned_indexes":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14],
      "internal_keychain_scanned_indexes":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
   },
   "transaction":{
      "status":"broadcasted",
      "txid":"0610ee05c50562865ded2893a89e5236ef9370b40f1933911525e97c22a7b00f"
   }
}

Before I continue applying this approach to other examples, I have a couple of questions:

  1. Does this approach align with the issue?
  2. Some examples already accept various options that change the output. In such cases, should I modify the code so that if -j is passed, any variant of the output also gets serialized into JSON?

Looking forward to your feedback! Thanks in advance🙌

@nymius
Copy link
Contributor Author

nymius commented Apr 11, 2025

Hi @PranavKumar20, thanks for working on this. Answering your questions:

  1. Your approach seems aligned.
  2. I think that will come up later during the review of your code. It will be done probably in a case-by-case basis. Remember not every output needs to be formatted in JSON, just the one that is produced as output of the command and not as logging or debugging info.
    For example, from your output, I would leave syncing format as it was before, because is more of a progress indicator than a final output of the command.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Discussion
Development

No branches or pull requests

6 participants