简体   繁体   中英

Filtering JSON API results minimally

Im attemtpting to filter results from an API query and was wondering what the best case scenario would be to accomplish this goal.

https://public-api.solscan.io/account/METAmTMXwdb8gYzyCPfXXFmZZw4rUsXX58PNsDg7zjL

Results in:

{"lamports":291171461600,"ownerProgram":"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA","type":"token_account","rentEpoch":283,"account":"METAmTMXwdb8gYzyCPfXXFmZZw4rUsXX58PNsDg7zjL","tokenInfo":{"name":"Solice","symbol":"SLC","price":1.28,"volume":1253400,"decimals":6,"tokenAuthority":null,"supply":"400000000000000","type":"token_address"}}

However the intended goal is to only have the value of "supply" show, which in this case would be 400000000000000 .

Would need to be as minimal of a solution as possible while being able to be machine read.

My first thought was filter it with JS but that is reliant on processing the JS.

Aside from parsing the JSON into an object, and then extracting the field, you can use a regex and speed it up a bit.

const rx = /"supply":"([^"]*)"/
const m = rx.exec(unparsedJson)
const result = m ? m[1] : undefined

https://regex101.com/r/Vx3Cp6/1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM