0

Given a table with jsonb field and a list of attribute names. How do I go about formatting a query to select all the attributes in the list?

I have a list with attribute name: Never say 'Never'

create table t(a jsonb);

insert into t values('{ "Never say ''Never''": "I won''t"}'::jsonb);

select a->>'Never say ''Never''' as "Never say 'Never'" from t;

Something like that, but no quotes for attribute names:

pgp.as.format(attrs.map((attr, i) => `a->>'$${i}~' as $${i}~`).join(', '), attrs);
Furkan Yavuz
  • 1,858
  • 5
  • 30
  • 51
  • Please format your code properly, and provide a clear example of the output that you expect, plus what you have tried. – vitaly-t May 19 '19 at 03:22
  • You are using `~` for use with quoted names, but asking for a no-quotes version? I don't understand what it is that you really want. You need to clarify your question, and make a simpler example of what you get and what you want. – vitaly-t May 19 '19 at 09:35
  • Let me rephrase: is there a way to format jsonb attribute names so apostrophes are properly escaped? – Michael Rybachenko May 19 '19 at 11:07
  • Not sure what properly means in your case, but perhaps the [open-value](https://github.com/vitaly-t/pg-promise#open-values) filter, if you want those open? – vitaly-t May 19 '19 at 11:09
  • Great, that worked like I expected. Thank you. – Michael Rybachenko May 19 '19 at 11:18

0 Answers0