Learn by product

Select the product you would like help with:

Martini Logo
Select the product you would like help with:
Community Q&A

Ask the Community

What I'm going for is that when I send a CSV that contains these data for example: 

  

testUser2,testFirst2,testMidd2,FirstLast2

and my initial data in my JSON file is this:

 

[
	{
		"username": "testuser",
		"name": {
			"givenName": "testFirst",
			"middleName": "testMidd",
			"familyName": "testLast"
		}
	}
]

It will be inserted into a JSON file like this one:

    

[
	{
		"username": "testuser",
		"name": {
			"givenName": "testFirst",
			"middleName": "testMidd",
			"familyName": "testLast"
		}
	},
	{
		"username": "testuser2",
		"name": {
			"givenName": "testFirst2",
			"middleName": "testMidd2",
			"familyName": "testLast2"
		}
	},
]

 How do I add an entry to the JSON file from CSV?


Given the requirements, you can Flat File Descriptor and a one-liner called FileMethods


The logic for the implementation will be like this:


  1. Using the flat file descriptor, extract the CSV values. Assuming that you have a properly formatted CSV and properly configured flat file descriptor, It will produce a Gloop model that contains properties based on your CSV input.
  2. Create a Gloop model representation of the JSON you are trying to insert to your CSV file.
  3. Create a Gloop Model representation of the JSON string you have in your file.
  4. Using FileMethods.writeStringToFile, write the mapped CSV values to the Gloop Model. The Gloop Method FileMethods.writeStringToFile accepts two arguments file and data. file is where you tell the method where your file is, and data is where you pass the content to be written in the file.