How can I convert a Text like "{"bitcoin":{"usd":18053.8}}" into an object or vector that I can use programatically?

How can I convert a Text like this

"{"bitcoin":{"usd":18053.8}}"

into a object like this or something like it that I can use programatically?

{bitcoin : {usd:18053.8}}

Have you looked at this thread? Http outcall response JSON processing in Motoko - #3 by Kyan

1 Like

Great. Will try it out today.

Would it be possible to add something like .fromJSON() to the base Text type to make it easier to use in the future?

1 Like

No idea, but I will ask in the Motoko team

import serdeJson "mo:serde/JSON";
    
type User = {
    name: Text;
    id: Nat;
};

let blob = serdeJson.fromText("{\"name\": \"bar\", \"id\": 112}");
let user : ?User = from_candid(blob);

assert user == ?{ name = "bar"; id = 112 };
4 Likes

So I’d expect Text.fromJSON() to return a Text value, not some structured type.

Maybe the library referenced above does what you want, but I can’t quite figure out how it works myself.

Thank you all. Using a partly manual solution for now but may return to it later.