mosh
mosh

Reputation: 544

How to store Json in a single column of a CSV file

Assuming i have this Json:

{"firstName":"myName","lastName":"lName"}

is there anyway to store it as a value of a single CSV column. (some sort of escaping?)

Example: Assuming i have an object with the properties:

id = "someId"
config = {"firstName":"myName","lastName":"lName"}
key = "someKey"

The desired CSV output is:

id         config                                             key
someId    {"firstName":"myName","lastName":"lName"}          someKey

Or

id         config                                                      key
someId    "{\"firstName\":\"myName\",\"lastName\":\"lName\"}"          someKey

If possible, how will such a CSV look like when you open it with a text editor? (meaning i am not interested in the tool producing this outcome, i'm interested in how the raw outcome looks like)

Upvotes: 8

Views: 18319

Answers (1)

Caius Jard
Caius Jard

Reputation: 74660

If your Json doesn't have commas it would work to just dump your Json into a comma separated line (because it wouldn't have any commas in to screw up the structure)

If it did you'd need to quote the start and end of the field and double up all the Json quotes within

If you want to make it manually, try eg

  //if the Json has commas
  id,config,key
  someId,"{""firstName"":""myName"",""lastName"":""lName""}",someKey

 
  //if the Json doesn't have commas
  id,config,key
  someId,{firstName:
myName},someKey

Upvotes: 18

Related Questions