Skip to content

Conversation

@bendrucker
Copy link
Member

@bendrucker bendrucker commented Oct 16, 2025

Fixes WalkExpressions to handle Terraform JSON files using array-based root syntax like [{"import": {...}}]. Previously these files would fail because JustAttributes() doesn't work with JSON arrays.

Issue

Terraform JSON files can use either object-based syntax {"resource": {...}} or array-based syntax [{"resource": {...}}]. The WalkExpressions method in the plugin SDK only supported object-based syntax.

When processing array-based JSON files, Body.JustAttributes() fails because HCL's JSON parser doesn't expose attributes at the array level. This caused WalkExpressions to return errors and skip processing these files entirely.

Closes terraform-linters/tflint#2279

Changes

  • Updates WalkExpressions to support array-based JSON syntax in addition to object-based syntax
  • Adds extractJSONKeys function to parse JSON bytes with encoding/json and extract attribute names from both objects and arrays of objects
  • Adds getJSONAttributes function that falls back to schema-based extraction when JustAttributes() fails
    • First attempts JustAttributes() for backward compatibility with object-based JSON
    • On failure, uses extractJSONKeys to discover attribute names
    • Builds dynamic schema and uses PartialContent to extract proper *json.expression objects
  • This ensures extracted expressions pass json.IsJSONExpression() checks used by rules

Testing

  • Adds test case for array-based JSON with multiple root-level objects
  • Verifies expressions are correctly extracted and walked for both resource and variable blocks

References

Adds support for JSON files with array-based root syntax like
[{"import": {...}}] which is used by Terraform 1.5+ for import
blocks. Previously, WalkExpressions would fail on these files
because JustAttributes() doesn't work with array-based JSON.

The fix implements a fallback mechanism that:
1. Tries JustAttributes() first (works for object-based JSON)
2. On failure, calls Content() to discover attribute names from
   error diagnostics
3. Builds a schema with discovered attributes
4. Uses PartialContent() to extract proper json.expression objects

This ensures expressions pass json.IsJSONExpression() checks and
can be properly walked by rules.
Replaces error message parsing approach with direct JSON decoding
using encoding/json. This is more reliable, faster, and cleaner.

- Uses standard library to extract keys from JSON
- Works for both object and array syntax
- No string parsing or Content() call needed
- All tests pass
Copy link
Member

@wata727 wata727 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@bendrucker bendrucker merged commit 943592a into master Oct 17, 2025
7 checks passed
@bendrucker bendrucker deleted the json-array-syntax branch October 17, 2025 01:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

TFLint errors on JSON syntax with root array of blocks

3 participants