Sumit Shrestha
Sumit Shrestha

Reputation: 919

How to insert json object containing $ as value in Cassandra

I have to add couple of values as currency in cassandara db through datastax one of which has $ as value. But it is failing.

INSERT INTO concept_value_set (concept_name,value_set) 
    VALUES ('@CurrencyType',{'Dollar', 'Usd', 'Euro', 
    'Pound', 'Frank', 'Renminbi', '$'});

This causes issue with the system.

I tried to escape $ using

INSERT INTO concept_value_set (concept_name,value_set) 
   VALUES ('@CurrencyType',{'Dollar', 'Usd', 'Euro', 
   'Pound', 'Frank', 'Renminbi', '$$'});

but it fails too.

Is there a way to escape $ here. I could not find any proper links on it.

Upvotes: 2

Views: 504

Answers (1)

Aaron
Aaron

Reputation: 57798

I don't think the dollar sign is your problem here. I think it's in how the JSON value is using single quotes. Rebuilding your table (based on the info provided), I came up with this structure:

CREATE TABLE concept_value_set (
  concept_name TEXT PRIMARY KEY,
  value_set TEXT);

And I was able to get this to work:

cassdba@cqlsh:stackoverflow> INSERT INTO concept_value_set JSON '{
    "\"concept_name\"":"@CurrencyType",
    "\"value_set\"":"Dollar, Usd, Euro, Pound, Frank, Renminbi, $"}';

Now, I can query the table for TEXT:

cassdba@cqlsh:stackoverflow> SELECT * FROM concept_value_set
                             WHERE concept_name='@CurrencyType';

 concept_name  | value_set
---------------+----------------------------------------------
 @CurrencyType | Dollar, Usd, Euro, Pound, Frank, Renminbi, $

(1 rows)

The table can also be queried for JSON, like this:

cassdba@cqlsh:stackoverflow> SELECT JSON value_set FROM concept_value_set
                             WHERE concept_name='@CurrencyType';

 [json]
---------------------------------------------------------------
 {"value_set": "Dollar, Usd, Euro, Pound, Frank, Renminbi, $"}

(1 rows)

You can find more info on working with JSON in Cassandra on the main Apache Cassandra project documentation site, as well as on DataStax's documentation site: (INSERT JSON, QUERY JSON).

Hope this helps.

Upvotes: 2

Related Questions