question

dominicvivek06_77810 avatar image
dominicvivek06_77810 asked Erick Ramirez edited

How do I insert data in nested JSON format?

Hi,

We have data in JSON with the below format -

{ "Event_Type": "Booked", "Car": { "RegId": "0961dbac-297d-424c-96ce-9f37f1707cd9" } }

{ "Event_Type": "Complete", "Car": { "RegId": "0961dbac-297d-424c-96ce-9f37f1707cd9" } }

{ "Event_Type": "Booked", "Car": { "RegId": "b32f7b0b-d077-44c4-a454-519a3f82c759" } }

{ "Event_Type": "Complete", "Car": {"RegId": "b32f7b0b-d077-44c4-a454-519a3f82c759"} }

{ "Event_Type": "Booked", "Car": { "RegId": "6fa0b439-0782-49e8-9ac3-78c275470516" } }

{ "Event_Type": "Complete", "Car": { "RegId": "6fa0b439-0782-49e8-9ac3-78c275470516"} }

we need to insert into cassandra with the below table structure

CREATE TABLE IF NOT EXISTS "car"."car_appt" (

"event_type" text,

"RegId" uuid,

"timetamp" timestamp

);

I am able to insert a json with no nested json, but the Car is inside a json structure.

cql
1 comment
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Erick Ramirez avatar image Erick Ramirez ♦♦ commented ·

@dominicvivek06_77810 apologies for the delay. I'll get a response to you shortly. Cheers!

0 Likes 0 ·

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez commented

@dominicvivek06_77810 the problem is that the JSON data does not match the table schema. You need to format the JSON such that it matches the layout in the schema.

It's no different to specifying the data with VALUES ( ... ). The only difference is that you're providing it in JSON format. Cheers!

2 comments Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

dominicvivek06_77810 avatar image dominicvivek06_77810 commented ·

@Erick Ramirez The JSON can't be reformatted. Its events dumped from another process.

0 Likes 0 ·
Erick Ramirez avatar image Erick Ramirez ♦♦ dominicvivek06_77810 commented ·

Unfortunately, it won't work the way you expect. The data has to be in the correct format for the INSERT to work as you want. Otherwise, it is working as expected.

Your application needs to parse the data and construct it in the right way before inserting to C*. Cheers!

0 Likes 0 ·