How should I format my documents as CSV
RavenDB uses JSON format for storing documents, thus the CSV lines representing documents should have a specific format.
There are three types of properties in JSON:
- Primitive: values that are numbers, strings, or booleans
- Nested Object: where the value of the property is a JSON object
- Array: an array of values that can either be primitives, nested objects, or arrays
Let's look at a sample JSON document:
"Name": "Import from CSV",
"Name": "Inner Object"
Name property is a primitive and should appear unescaped in the CSV like so:
Import from CSV
NestedObject property is a nested JSON object and as such should be decomposed into multiple properties - one for each nested property.
The decomposition rule goes as follows:
the name of the parent property].[
name of the inner object property]
The import process will combine properties with the same prefix back into one JSON object.
ArrayObject property is an array and as such contains multiple values. These should be escaped as a string like so:
The last thing we need in order for an import to work is to add the
collection property. If we don't, the name of the collection will derive from the CSV file name.
If we want to import the document with a specific
id we need to include an
@id property too.
The complete CSV line should look like this:
Samples/1-A,Import from CSV,Inner Object,"[1,2,3,4]",Samples
Note that the first line of a .csv file applies to the header rows.
It names the column headers in a table, but in a .json document, it names the key properties.