Solved

How get this query ?

Posted on 2014-07-23
7
348 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

738 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