0

Have a project for work & my SQL skills are improving, but I still struggle with basic stuff every now and then. I need to get 'fundingCode' to show up as a string rather than a number as it is now.

(i.e. currently the outcome is '"fundingCode": 100001' & I need it to show up as '"fundingCode": "100001"

cursor json_index_data (c_org_code VARCHAR2) is 
    select json_object(
            '_dtoName' VALUE 'AeSFndOrganization',
            'companyId' VALUE oa.location_desc,
            'deptId' VALUE OA.DEPT,
            'ocCode' VALUE OA.ORGANIZATION_LEVEL_6,
             nvl('seq', 1) VALUE imi.seq, 
            'fundingCode' VALUE (I.ACCOUNT_INDEX)
    FORMAT JSON)
    as json_row_value
2
  • Just gave it a shot, throwing an error unfortunately. Really appreciate the suggestion though. Commented Dec 20, 2022 at 17:49
  • Sorry, I didn't realize this was Oracle. Commented Dec 20, 2022 at 18:28

2 Answers 2

1

The way you would typically do a select statement resulting in JSON would be like this:

select json_object(
        key '_dtoName' is 'AeSFndOrganization',
        key 'companyId' is oa.location_desc,
        key 'deptId' is OA.DEPT,
        key 'ocCode' is OA.ORGANIZATION_LEVEL_6,
        key 'seq' is imi.seq, 
        key 'fundingCode' is to_char(I.ACCOUNT_INDEX)
      ) as YOUR_JSON_ALIAS
   from YOUR_TABLENAME;

So you define your key on the left and your value (with is) on the right. I don't think that would act any differently in a cursor if you just put cursor json_index_data is in front of it.

If you need to convert a value, use the typical conversion functions like to_char(the_value) or to_number(the_string), etc. That's what I did in the sample query above for:

...
key 'fundingCode' is to_char(I.ACCOUNT_INDEX)
...
Sign up to request clarification or add additional context in comments.

Comments

0

So a coworker managed to give me a pretty good explanation. I had previously tried to use concatenation, but I was using bad syntax. Here is how it was finally accomplished.

cursor json_index_data (c_org_code VARCHAR2) is 
    select json_object(
            'multitenantId' VALUE '1',
            '_dtoName' VALUE 'AeSFndOrganization',
            'companyId' VALUE oa.campus_desc,
            'deptId' VALUE OA.DEPT,
            'ocCode' VALUE OA.ORGANIZATION_LEVEL_6,
            nvl('seq', 1) VALUE imi.seq,                 
            'fundingCode' VALUE '"' || I.ACCOUNT_INDEX || '"'
    FORMAT JSON)
    as json_row_value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.