Hello Gayan,

Do you work in the carbon-apimgt or the product-apim??

If you are working in the carbon-apimgt, there is a already written native
function for this, (since ballerina 0.89 does not support this operation).
Here is an example,
json jsonObject = util:parse(stringObject);

In case if you are working in the product-apim, you can add the followin to
your pom file,
     <groupId>org.wso2.carbon.apimgt</groupId>
    <artifactId>org.wso2.carbon.apimgt.ballerina.native</artifactId>
as a new dependency, and you should be able to use the above method as
usual.

Reagards.

On Mon, Oct 30, 2017 at 6:02 PM, Gayan Chamara <gaya...@wso2.com> wrote:

> Hi Anupama,
> Unfortunately, that too does not seem to work. Ballerina throws an error
> when I try to use *jsonVal.ab* or *jsonVal["ab"]*.
>
> However, I've come up with the following code and it seems to work.
> That'll do for now.
>
> message m = {};
> messages:setStringPayload(m, str);
> json j = messages:getJsonPayload(m);
>
> Thanks,
> Gayan.
>
>
> On Mon, Oct 30, 2017 at 5:40 PM, Anupama Pathirage <anup...@wso2.com>
> wrote:
>
>> Hi,
>>
>> In earlier versions of ballerina following should work for string to json
>> and json to string conversions.
>>
>> import ballerina.lang.system;
>> import ballerina.lang.jsons;
>>
>> function main(string[] args) {
>>     string s1 = "{'ab':'ddd'}";
>>     json jsonVal = s1;
>>     system:println(jsonVal);
>>     string s2 = jsons:toString(jsonVal);
>>     system:println(s2);
>> }
>>
>> Thanks,
>>
>> Anupama
>>
>>
>>
>> On Mon, Oct 30, 2017 at 5:07 PM, Gayan Chamara <gaya...@wso2.com> wrote:
>>
>>> Hi Rajith & Suthagar,
>>> I tried the suggested method, but no luck. It gives the following error
>>> message,
>>>
>>> *incompatible types: 'string' cannot be converted to 'json'*
>>>
>>>
>>> I'm restricted to ballerina 0.89 because API Manager has not moved to a
>>> newer version yet.
>>>
>>> Regards,
>>> Gayan.
>>>
>>> On Mon, Oct 30, 2017 at 4:57 PM, Suthagar Kailayapathy <
>>> sutha...@wso2.com> wrote:
>>>
>>>> Hi Gayan,
>>>>
>>>> Can you please take a look at here(last one),
>>>> https://groups.google.com/forum/#!topic/ballerina-dev/DIWQIULUvSw
>>>>
>>>> string s = "somejson";
>>>> json j;
>>>> Error e;
>>>> j, err = <json> s;
>>>>
>>>> Thanks.
>>>>
>>>> On Mon, Oct 30, 2017 at 4:39 PM, Gayan Chamara <gaya...@wso2.com>
>>>> wrote:
>>>>
>>>>> Hi,
>>>>> Is there a way to convert JSON string into ballerina json type in
>>>>> ballerina 0.89? I couldn't find a suitable method in the docs.
>>>>>
>>>>> Thanks,
>>>>> Gayan.
>>>>>
>>>>> --
>>>>> Regards,
>>>>>
>>>>> *Gayan Chamara*
>>>>>
>>>>> *Software Engineering Intern*
>>>>> *WSO2 Inc.*
>>>>>
>>>>>
>>>>> *Email: gaya...@wso2.com <gaya...@wso2.com> *
>>>>> *Mobile : **+94 71 728 5827 <+94%2071%20728%205827>*
>>>>> <http://wso2.com/signature>
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>>
>>>> *K.Suthagar*
>>>> Software Engineering Intern | WSO2
>>>> LinkedIn: https://www.linkedin.com/in/ksuthagar
>>>> <http://suthagar.inncaps.com/>
>>>>
>>>> Email: sutha...@wso2.com
>>>> Mobile : +94779543968 <+94%2077%20954%203968>
>>>> Web: http://wso2.com
>>>>
>>>>
>>>
>>>
>>> --
>>> Regards,
>>>
>>> *Gayan Chamara*
>>>
>>> *Software Engineering Intern*
>>> *WSO2 Inc.*
>>>
>>>
>>> *Email: gaya...@wso2.com <gaya...@wso2.com> *
>>> *Mobile : **+94 71 728 5827 <+94%2071%20728%205827>*
>>> <http://wso2.com/signature>
>>>
>>
>>
>>
>> --
>> Anupama Pathirage
>> Technical Lead
>> WSO2, Inc.  http://wso2.com/
>> Email: anup...@wso2.com
>> Blog: http://mycodeideas.blogspot.com
>> Mobile:+94 71 8273 979
>>
>>
>>
>>
>
>
> --
> Regards,
>
> *Gayan Chamara*
>
> *Software Engineering Intern*
> *WSO2 Inc.*
>
>
> *Email: gaya...@wso2.com <gaya...@wso2.com> *
> *Mobile : **+94 71 728 5827 <+94%2071%20728%205827>*
> <http://wso2.com/signature>
>



-- 
*Sabeena Kumarawadu* | Software Engineering Intern
WSO2 Lanka (Pvt) Ltd.
#20, Palm Grove, Colombo 03, Sri Lanka
Mobile: +94 71 0372856
Email: sabe...@wso2.com
[image: http://wso2.com/signature] <http://wso2.com/signature>
_______________________________________________
Dev mailing list
Dev@wso2.org
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to