You can also use standard Python looping techniques on this list to access each element in this list.
To work with an individual element’s contents, you can use standard dictionary operations on the element.
For example:
Python
Copy
Ask AI
# ...res = await client.general.partition_async(request=req)for element in res.elements: # Do something with each element, for example: save_element_to_database(f"{element["element_id"]}") save_element_to_database(f"{element["text"]}") save_element_to_database(f"{element["metadata"]["filename"]}\n")# ...
To serialize this list as JSON, you can:
Use the elements_from_dicts function to convert the list of key-value dictionaries (Iterable[Dict[str, Any]]) into a list of elements (Iterable[Element]).
Use the elements_to_json function to convert the list of elements into a JSON-formatted string and then print or save that string.
For the Unstructured JavaScript/TypeScript SDK, calling an UnstructuredClient object’s general.partition method returns a Promise<PartitionResponse> object.
This PartitionResponse object’s elements property contains an Array of string-value objects ({ [k: string]: any; }[]). For example:
TypeScript
Copy
Ask AI
// ...client.general.partition({ partitionParameters: { files: { content: data, fileName: inputFilepath }, strategy: Strategy.HiRes, splitPdfPage: true, splitPdfAllowFailed: true, splitPdfConcurrencyLevel: 15 }}).then((res) => { if (res.statusCode == 200) { // Do something with the elements, for example: saveElementsToFile(res) }} // ...
You can also use standard Array techniques such as forEach to access each object in this array. For example:
TypeScript
Copy
Ask AI
// ...client.general.partition({ partitionParameters: { files: { content: data, fileName: inputFilepath }, strategy: Strategy.HiRes, splitPdfPage: true, splitPdfAllowFailed: true, splitPdfConcurrencyLevel: 15 }}).then((res) => { if (res.statusCode == 200) { res.forEach(element => { // Do something with each element, for example: saveElementToDatabase(`${element["element_id"]}`) saveElementToDatabase(`${element["text"]}`) saveElementToDatabase(`${element["metadata"]["filename"]}`) } }} // ...
To serialize this list as JSON, you can use the standard JSON.stringify function to serialize it to JSON-formatted string and the Node.js fs.WriteFileSync function to save it as a file. For example: