Solved

How get this query ?

Posted on 2014-07-23
7
351 Views
Last Modified: 2014-07-23
Hi expert,

below i have query
(SELECT regexp_replace('{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}','^[{"]+|[}"]+$') AS rec
FROM dual
);

Open in new window


result:Result:1":1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":
Expected Result: 1":{"ACC":1,"YBT":"damby}damby
                            2":{"ACC":1,"YBT":"damby}damby
                          3":{"ACC":1,"YBT":


Regards
Thomos
0
Comment
Question by:deve_thomos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 74

Expert Comment

by:sdstuber
ID: 40215072
please as I explained in your other question:

Plase don't post a query that doesn't work and then just assume I'll know what you want it do.

Give me INPUT DATA  (a query is not data)
and expected OUTPUT DATA

along with the parsing rules.

For example of the types of rules I might need:  can you have more than 3 substrings?  How are substrings identified?  Do you need multiple depth searching?

I'm going to assume from your previous question and your query that you want to do {} and " stripping at the outermost level but you haven't specified it here.  

When you post a query and then ask how to get "the query"  you're not giving good enough information.
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 40215157
here's an example of what I would expect...

INPUT:  '{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}'

Parsing Rule1:  The data is json structured
Parsing Rule 2: Pull each json node out of the string that starts with a { and numeric label "1", "2", "3" and ends with }}
Parsing Rule 3: Return each output as a distinct row with leading and trailing {} and " removed

OUTPUT of 3 rows
1":{"ACC":1,"YBT":"damby}damby
2":{"ACC":1,"YBT":"damby}damby
3":{"ACC":1,"YBT":


Using this example, I would write the query like this...

SELECT REGEXP_REPLACE(
           REGEXP_SUBSTR(
               '{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}',
               '"[0-9]+".+?}}',
               1,
               LEVEL
           ),
           '^[{"]+|[}"]+$'
       )
           AS rec
  FROM DUAL
CONNECT BY LEVEL <= 3;

Open in new window


That produces the expected results

HOWEVER - since I'm guessing at your parsing rules and structures of your input it doesn't mean it's correct as a general solution, it will only work for this particular string and possibly others with minor changes.
0
 

Author Comment

by:deve_thomos
ID: 40215420
Hi sdstuber,
in this case   you are giving  CONNECT BY LEVEL <= 3;  suppose  my out put is having  10 rows  or more than that  how i will handle that .

Regrads
Thomos
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 74

Expert Comment

by:sdstuber
ID: 40215431
assuming the 10 rows have the same parsing rules that I guessed at then

 CONNECT BY LEVEL <= 3;

becomes

 CONNECT BY LEVEL <= 10;

If the parsing rules are different than my guess then you'll have to do something else but I can't begin to guess what.  

I really am guessing - you haven't described at all what your parsing rules are or confirmed the correct input.

I ask for the same things repeatedly (input, parsing rules, expected output) because it's important.  Trusting me to guess correctly is not a reliable methodology for you.
0
 

Author Comment

by:deve_thomos
ID: 40215454
Hi Sdstuber,
I am parsing some json data , so it can be  one row or it can more than one  row. exact row we cannot say .
in this case  i cannot fix that  CONNECT BY LEVEL <= 10 or CONNECT BY LEVEL <= 3; Then how i will handle this things

if you want  more information  please let me know.

Regards
Thomos
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 40215464
again - assuming MY parsing rules guesses are correct (you haven't confirmed or described your rules) AND the structure of the data will match those rules every time then try something like this.


SELECT *
  FROM (SELECT REGEXP_REPLACE(
                   REGEXP_SUBSTR(
                       '{"1":{"ACC":1,"YBT":"damby}damby"}},"2":{"ACC":1,"YBT":"damby}damby"}},"3":{"ACC":1,"YBT":""}}',
                       '"[0-9]+".+?}}',
                       1,
                       LEVEL
                   ),
                   '^[{"]+|[}"]+$'
               )
                   AS rec
          FROM DUAL
        CONNECT BY LEVEL <= 10)
 WHERE rec IS NOT NULL;


If 10 isn't sufficient for all cases, make it something larger.
Given the 4k limit on varchar2 fields (at least prior to 12c) and given the structure of the json nodes,  it would be impossible for the value to need to be more than 500, but for efficiency you should pick something smaller but still big enough.
0
 

Author Closing Comment

by:deve_thomos
ID: 40215486
Thank u a lot....
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question