Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to represent a multi-line string of JSON? #63180

Open
Alex-Cheng opened this issue Apr 30, 2024 · 5 comments
Open

How to represent a multi-line string of JSON? #63180

Alex-Cheng opened this issue Apr 30, 2024 · 5 comments

Comments

@Alex-Cheng
Copy link
Contributor

I want to write a SQL like following:

set param_json1={
    "a":1,
    "b":2,
    ... ...
}

select foo({json1:String});

But the SQL does not work. Is there any approach to do this?

Thank you in advance.

@Alex-Cheng Alex-Cheng added the question Question? label Apr 30, 2024
@alexey-milovidov
Copy link
Member

alexey-milovidov commented May 1, 2024

In your example, the json1 parameter has a String type, which means that it has to be provided as a String, e.g.,

set param_json1='{
    "a":1,
    "b":2
}'

select foo({json1:String});

Surprisingly to me, it produces an error:

Code: 457. DB::Exception: Value {
    "a":1,
    "b":2
} cannot be parsed as String for query parameter 'json1' because it isn't parsed completely: only 1 of 24 bytes was parsed: {. (BAD_QUERY_PARAMETER)

@evillique
Copy link
Member

param_ settings are translated into String type after parsing automatically, so you can use it after as a String in substitution.

However, these parameters must be a literal, Array/Map/Tuple of literals, or a combination of them written in ClickHouse syntax. In ClickHouse double quotes (") are used for identifiers and you can't have those inside a literal.

Setting parameters like this is supported:

SET param_json1={
    'a':1,
    'b':2
};

SELECT {json1:String} AS str, {json1:Map(String, UInt64)} AS map;

┌─str────────────┬─map───────────┐
│ {'a':1, 'b':2} │ {'a':1,'b':2} │
└────────────────┴───────────────┘

If the parameter you are trying to set has some other format then you will have to use String literal. Inside this literal the parameter must be in "escaped" format (https://clickhouse.com/docs/en/interfaces/http#tabs-in-url-parameters).

So in your example it would be:

SET param_json1='{\\n    "a":1,\\n    "b":2,\\n};'

SELECT {json1:String} AS str;

┌─str─────────────────────┐
│ {
    "a":1,
    "b":2,
}; │
└─────────────────────────┘

@alexey-milovidov
Copy link
Member

@evillique It is ok to require Escaped format in HTTP interface and in command line arguments, because it is the only way to distinguish between a string and NULL.

But when the parameter is provided as an explicit string literal, it is questionable.

@Alex-Cheng
Copy link
Contributor Author

Alex-Cheng commented May 4, 2024

@evillique It is ok to require Escaped format in HTTP interface and in command line arguments, because it is the only way to distinguish between a string and NULL.

But when the parameter is provided as an explicit string literal, it is questionable.

Thank you for responding my question. Actually I have a very long JSON where strings are double-quoted(because it is standard JSON format). So if I cannot use multi-line string as parameter nor double-quote in JSON, then it would be inconvenient.

@Alex-Cheng
Copy link
Contributor Author

@evillique It is ok to require Escaped format in HTTP interface and in command line arguments, because it is the only way to distinguish between a string and NULL.
But when the parameter is provided as an explicit string literal, it is questionable.

Thank you for responding my question. Actually I have a very long JSON where strings are double-quoted(because it is standard JSON format). So if I cannot use multi-line string as parameter nor double-quote in JSON, then it would be inconvenient.

I have tried more. The RapidJSONParser cannot parse a JSON with single-quoted key, such as {'A':1, 'B':2}. If I use standard JSON format, such as {"A":1, "B":2}, parsing succeeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants