0

I have a redshift table "person" in which a particular column has data something like this

[{"attributeName":"name","attributeMetadata":null,"attributeValue":"KitchenAid - 7-Speed Hand Mixer - White","attributeImageType":"PRODUCT","attributeStatusCodes":[]},
{"attributeName":"title","attributeMetadata":null,"attributeValue":"KitchenAid","attributeImageType":"PRODUCT","attributeStatusCodes":[]},

{"attributeName":"address","attributeMetadata":null,"attributeValue":"address","attributeImageType":"PRODUCT","attributeStatusCodes":[]},

{"attributeName":"PIN CODE","attributeMetadata":null,"attributeValue":"32110","attributeImageType":"IMG","attributeStatusCodes":[]}]

I would like to extract only the dictionary/json/substring containing PIN CODE (see below)

{"attributeName":"PIN CODE","attributeMetadata":null,"attributeValue":"32110","attributeImageType":"IMG","attributeStatusCodes":[]}

I tried the following query and it is giving the following error

select distinct regexp_substr(attributes,'.*({.*?"attributeName":"PIN CODE".*?}).*') from person ;

ERROR:  Invalid content of repeat range
DETAIL:  
  -----------------------------------------------
  error:  Invalid content of repeat range
  code:      8002
  context:   T_regexp_init
  query:     528401
  location:  funcs_expr.cpp:130
  process:   query2_40 [pid=12603]
  -----------------------------------------------

I guess the problem is occurring because of multiple attributeName in a single column. Is their a way to achieve the desired result.

4
  • You should escape curly braces as \{, otherwise it's assumed as a quantifier like \w{2,5}. That's why it throws the error "Invalid content of repeat range" Commented Nov 4, 2015 at 8:26
  • @Mario Sorry didnt worked, got the same error Commented Nov 4, 2015 at 8:30
  • Any chance you can create a fiddle? Commented Nov 4, 2015 at 8:55
  • Start by providing an exact table definition. Commented Nov 4, 2015 at 13:02

1 Answer 1

2

I am not sure if I understood you correctly, but you can try to use LIKE:

    select * from person where attributes LIKE '%"attributeName":"PIN CODE"%';
Sign up to request clarification or add additional context in comments.

1 Comment

@IvanNo I want the subtring {"attributeName":"PIN CODE","attributeMetadata":null,"attributeValue":"32110","attributeImageType":"IMG","attributeStatusCodes":[]}. The string above(in the question) is in a single column

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.