Skip to content

Instantly share code, notes, and snippets.

@alexmccabe
Created June 17, 2020 09:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexmccabe/d39f682f81b0e35e1badbb15f6b4a098 to your computer and use it in GitHub Desktop.
Save alexmccabe/d39f682f81b0e35e1badbb15f6b4a098 to your computer and use it in GitHub Desktop.
Convert object data into FormData
function createFormData(data, previousKey, formData = new FormData()) {
if (isPlainObject(data)) {
Object.keys(data).forEach(key => {
const value = data[key];
let newKey = key;
if (isPlainObject(value)) {
return createFormData(value, key, formData);
}
if (previousKey) {
newKey = `${previousKey}[${key}]`;
}
if (isArray(value)) {
value.forEach((item, index) => {
if (isPlainObject(item)) {
createFormData(item, `${newKey}[${index}]`, formData);
} else {
formData.append(`${newKey}[${index}]`, item);
}
});
} else {
formData.append(newKey, value);
}
});
} else if (previousKey) {
formData.append(previousKey, data);
}
return formData;
}
const input = require('./input.json');
console.log(createFormData(input));
{
"title": "A Title",
"body": "Some details",
"images": [
{
"name": "C0A5A8EA-6935-4148-B166-33FD198F6672.jpg",
"type": "image/jpeg",
"uri": "/Users/alexmccabe/Library/Developer/CoreSimulator/Devices/71642DC3-655E-485A-BA3F-DC32FB7D3C55/data/Containers/Data/Application/22091589-91C7-4593-8323-F7AA0F725FB0/tmp/react-native-image-crop-picker/C0A5A8EA-6935-4148-B166-33FD198F6672.jpg"
},
{
"name": "2A632728-3BA9-466E-9732-417D74304674.jpg",
"type": "image/jpeg",
"uri": "/Users/alexmccabe/Library/Developer/CoreSimulator/Devices/71642DC3-655E-485A-BA3F-DC32FB7D3C55/data/Containers/Data/Application/22091589-91C7-4593-8323-F7AA0F725FB0/tmp/react-native-image-crop-picker/2A632728-3BA9-466E-9732-417D74304674.jpg"
}
],
"categoryId": 1,
"tags": [
{
"id": 8,
"type": "genre",
"name": "2-Tone"
},
{
"id": 11,
"type": "genre",
"name": "8-Bit / BitPop"
},
{
"id": 13,
"type": "genre",
"name": "Acid Breaks"
}
]
}
{
"_parts": [
[
"title",
"A Title"
],
[
"body",
"Some details"
],
[
"images[0][name]",
"C0A5A8EA-6935-4148-B166-33FD198F6672.jpg"
],
[
"images[0][type]",
"image/jpeg"
],
[
"images[0][uri]",
"/Users/alexmccabe/Library/Developer/CoreSimulator/Devices/71642DC3-655E-485A-BA3F-DC32FB7D3C55/data/Containers/Data/Application/22091589-91C7-4593-8323-F7AA0F725FB0/tmp/react-native-image-crop-picker/C0A5A8EA-6935-4148-B166-33FD198F6672.jpg"
],
[
"images[1][name]",
"2A632728-3BA9-466E-9732-417D74304674.jpg"
],
[
"images[1][type]",
"image/jpeg"
],
[
"images[1][uri]",
"/Users/alexmccabe/Library/Developer/CoreSimulator/Devices/71642DC3-655E-485A-BA3F-DC32FB7D3C55/data/Containers/Data/Application/22091589-91C7-4593-8323-F7AA0F725FB0/tmp/react-native-image-crop-picker/2A632728-3BA9-466E-9732-417D74304674.jpg"
],
[
"category_id",
1
],
[
"tag_ids[0]",
8
],
[
"tag_ids[1]",
11
],
[
"tag_ids[2]",
13
]
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment