JSON encode

Hi

I am calling the stripe api from the cloud code using the code below, the body is returned as a string, is there anyway I can decode that in the cloud api rather than sending it back to Corona and getting Corona to do it (which does work), basically I want to call json.decode on the body so I have the information in the cloud api.

The reason is I’m trying to do this is to minimize the backwards and forwards of many calls to the server.

local api\_key = "sk\_test\_1234567890" local description = "Test" local answer = coronium.user.getUser( in\_data.userid ) if not answer.error then local email = answer.result.email coronium.log( answer.result.email ) local newCustomer = "email="..email.."&description="..description local header = { ["Authorization"] ="Bearer "..api\_key, } local answer = coronium.network.request( 'https://api.stripe.com/v1/customers', newCustomer, 'POST', header ) coronium.log( type(answer.result.body) ) coronium.output( {result = answer.result.body} ) else coronium.output( answer ) end

While I have not used Coronium and I only saw a JSON encoding function looking through the docs, perhaps:

local json = require( “json” )

would work and then you would have json.decode() available. I’m guessing here, hopefully @develephant will be on soon and give you a real answer.

Rob

Thanks Rob, but I had already tried that … it returns “module ‘json’ not found”

ok I just uploaded a version of dkjson to the server and required that and that seems to work nicely :slight_smile:

Hi,

Sorry for late reply. Stripe will return a JSON encoded response (as you’ve found), this is a one-step call built into the Coronium SDK.

http://docs.coronium.io/en/latest/server/HTTP/#networkpostjson

This method posts and returns a Lua table converted from the JSON response.

You might wonder why there is seemingly no “encode/decode” method. That was a design decision because Coronium relies on a unified pattern of decode/encode throughout the system, so that the components can easily inter-operate. This is done automatically. If a “manual” conversion is not handled correctly and gets pushed back into that pipeline, failures will occur. Hence the above method, and the “backgrounding” of the lower level calls.

Coronium uses cjson in a “safe” way. This means that Coronium won’t throw a runtime error if it cannot convert the data. Instead you will receive the standard “Answer” table with the errorString telling you why it would not convert.

If you need access to this at a lower level its:

[lua]

local answer = coronium.json.encode( data )

if answer.error then

  print( answer.errorString )

else

  print( answer.result.some_var )

end

[/lua]

and to decode…

[lua]

local answer = coronium.json.decode( data )

– see above code

[/lua]

I would advise removing the dkjson. The consequences are yours, and yours alone. :slight_smile:

Cheers.

Thanks Chris,

I changed to using coronium.json.decode and it works fine. (dkjson removed)

Thank you

Chris

Not a problem, sorry for the confusion on that one.

Cheers.

While I have not used Coronium and I only saw a JSON encoding function looking through the docs, perhaps:

local json = require( “json” )

would work and then you would have json.decode() available. I’m guessing here, hopefully @develephant will be on soon and give you a real answer.

Rob

Thanks Rob, but I had already tried that … it returns “module ‘json’ not found”

ok I just uploaded a version of dkjson to the server and required that and that seems to work nicely :slight_smile:

Hi,

Sorry for late reply. Stripe will return a JSON encoded response (as you’ve found), this is a one-step call built into the Coronium SDK.

http://docs.coronium.io/en/latest/server/HTTP/#networkpostjson

This method posts and returns a Lua table converted from the JSON response.

You might wonder why there is seemingly no “encode/decode” method. That was a design decision because Coronium relies on a unified pattern of decode/encode throughout the system, so that the components can easily inter-operate. This is done automatically. If a “manual” conversion is not handled correctly and gets pushed back into that pipeline, failures will occur. Hence the above method, and the “backgrounding” of the lower level calls.

Coronium uses cjson in a “safe” way. This means that Coronium won’t throw a runtime error if it cannot convert the data. Instead you will receive the standard “Answer” table with the errorString telling you why it would not convert.

If you need access to this at a lower level its:

[lua]

local answer = coronium.json.encode( data )

if answer.error then

  print( answer.errorString )

else

  print( answer.result.some_var )

end

[/lua]

and to decode…

[lua]

local answer = coronium.json.decode( data )

– see above code

[/lua]

I would advise removing the dkjson. The consequences are yours, and yours alone. :slight_smile:

Cheers.

Thanks Chris,

I changed to using coronium.json.decode and it works fine. (dkjson removed)

Thank you

Chris

Not a problem, sorry for the confusion on that one.

Cheers.