0

I have a json file that is already loaded via an LWC app. I'd like to be able to perform some operations on the file using the JSON and JSONParser classes after.

I am unable to find basic file open/read methods. Are those types of operations available in Apex?

2
  • Where is your file actually stored? A ContentDocument? a Document? an Attachment? a Static Resource? Apex can read files that are stored in the database, but it's not very well-suited for extensive content processing.
    – David Reed
    Jan 20 at 1:37
  • Hi David - for testing I had just saved it to Files Owned by Me. Where I upload it isn't too important to me except they may be on the order of 100-300MB. I can also process on the .NET side and then just insert but I wanted to see if server-side may be a viable option. Jan 20 at 1:47
3

Per your comment:

[the files] may be on the order of 100-300MB

You can't process anything close to that size in Apex. The heap limit for most asynchronous code is 12MB.

While in general Apex is poorly suited to processing file data, you can do it by querying the data. For example, to get Content Files', well, content, you can query

SELECT VersionData FROM ContentVersion WHERE ...

which will result in a Blob instance that, provided it's UTF-8, you can convert to text and process - provided you can stay within governor limits.

For intensive processing or large file sizes, I always recommend doing it off-platform or on the frontend in JavaScript. In the future [safe harbor!] you may be able to consider Salesforce Functions (formerly known as Evergreen) as an alternative for such workloads.

1
  • Thanks again. Yes I'm aware of the limits and I do make use of async processing of larger data feeds via REST, but this is a different situation. At least I know I wasn't overlooking an Apex File API. I'll just stick to the .NET processing for this part then. Cheers Jan 20 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.