FileMaker JSON解析循环迭代时记录被覆盖问题求助
It sounds like you're hitting two common pitfalls with FileMaker JSON parsing and record creation—let's break this down and fix it step by step.
1. Check if Your ID Field is a Global Field
The most likely culprit here is that your grievanceSubmissions::ID field is set to Global Storage. Global fields in FileMaker share their value across all records in the table. So every time you set the ID in a new record, you're overwriting the value for all existing records, which results in all records showing the last ID from your JSON array.
How to Fix:
- Open Manage Database (File > Manage > Database)
- Go to the Tables tab, select
grievanceSubmissions - Go to the Fields tab, find the
IDfield - Uncheck the Global storage option (under the "Options" button or in the field properties)
- Save your changes and re-run the script.
2. Fix Your Submission Count Calculation
Your current script uses JSONListKeys to count submissions, but submissions is an array (not an object with named keys). JSONListKeys returns the keys of an object, so it will return an empty list for an array—meaning your $grievanceCount might be incorrect (or you're relying on a lucky accident in debugging!).
How to Fix:
Replace your count calculation with one of these two options:
Option A: Use JSONListValues (count array elements directly)
Set Variable [ $grievanceCount ; Value: ValueCount ( JSONListValues ( $json ; "submissions" ) ) ]
JSONListValues returns each element in the array as a separate line, so ValueCount gives you the correct number of submissions.
Option B: Use the "total" value from your JSON (cleaner, since it's provided)
Your JSON already includes a total field with the count of submissions, so you can use that directly:
Set Variable [ $grievanceCount ; Value: JSONGetElement ( $json ; "total" ) ]
3. Optional: Add Error Checking
To avoid unexpected behavior if the URL request fails, add a check after Insert from URL to ensure $json isn't empty:
Insert from URL [ Select; with dialog: Off ; $json; "web address"] If [ IsEmpty ( $json ) ] Show Custom Dialog [ "Error" ; "Failed to retrieve JSON data." ] Exit Script [ Text Result: "Error" ] End If
Revised Script
Putting it all together, your fixed script would look like this:
Insert from URL [ Select; with dialog: Off ; $json; "web address"] If [ IsEmpty ( $json ) ] Show Custom Dialog [ "Error" ; "Failed to retrieve JSON data." ] Exit Script [ Text Result: "Error" ] End If Set Variable [ $grievanceCount ; Value: JSONGetElement ( $json ; "total" ) ] Set Variable [ $i; Value: 0 ] If [ $grievanceCount > 0 ] Loop New Record/Request Set Field [ grievanceSubmissions::ID ; JSONGetElement ( $json ; "submissions[" & $i & "]id" ) ] Commit Records/Requests [ With dialog: Off ] Set Variable [ $i ; Value: $i + 1 ] Exit Loop If [ $i ≥ $grievanceCount ] End Loop End If
This should resolve the issue where all records end up with the last ID—you'll now get a unique record for each submission in your JSON array.
内容的提问来源于stack exchange,提问作者Dskip1199

