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
Solved

How get this query ?

Posted on 2014-07-23
7
343 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and the…
This video shows information on the Oracle Data Dictionary, starting with the Oracle documentation, explaining the different types of Data Dictionary views available by group and permissions as well as giving examples on how to retrieve data from th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

790 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