# Importing "terraform show -json \<file\>" to python

**URL:** <https://discuss.hashicorp.com/t/importing-terraform-show-json-file-to-python/5793>\
**Category:** Terraform\
**Created:** [February 8, 2020, 8:22pm UTC](https://discuss.hashicorp.com/t/importing-terraform-show-json-file-to-python/5793 "2020-02-08T20:22:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SQLJames](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/sqljames/32/4532_2.png) [@SQLJames](https://discuss.hashicorp.com/u/SQLJames)\
**Post date:** [February 8, 2020, 8:22pm UTC](https://discuss.hashicorp.com/t/importing-terraform-show-json-file-to-python/5793/1 "2020-02-08T20:22:32Z")

</div>

I am getting an error when trying to import the terraform json output from the "terraform show -json " command. My steps to replicate are as follows:

- terraform plan -out .\plan.state.tfstate
- terraform show -json .\plan.state.tfstate \> plan.state.show.json

then run the following code in a python script.

```auto
import json
#terraform plan -out .\plan.state.tfstate 
#terraform show -json .\plan.state.tfstate > plan.state.show.json
with open('plan.state.show.json', 'r') as jsonfile:
    jsonfile.seek(0)
    data = json.load(jsonfile)
print(json.dumps(data, indent=4))

```

This returns the following error

```auto
Traceback (most recent call last):
  File ".\terraform-validate.py", line 15, in <module>
    data = json.load(jsonfile)
  File "C:\Python38\lib\json\ __init__.py", line 293, in load
    return loads(fp.read(),
  File "C:\Python38\lib\json\ __init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "C:\Python38\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python38\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

```

I am able to import this output into powershell without issue. But I was wondering if someone has run into this prior.

Terraform v0.12.20

- provider.aws v2.47.0

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [February 11, 2020, 1:35am UTC](https://discuss.hashicorp.com/t/importing-terraform-show-json-file-to-python/5793/2 "2020-02-11T01:35:35Z")

</div>

Hi @SQLJames,

Does the content of your `plan.state.show.json` look like valid JSON if you inspect it directly in a text editor?

I’m wondering if perhaps the command failed for some reason and so the `plan.state.show.json` file is either empty or contains some non-JSON content, which the Python JSON parser then fails to read.

---

<div class="post-metadata">

**Author:** ![mcalr3](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/mcalr3/32/22568_2.png) [@mcalr3](https://discuss.hashicorp.com/u/mcalr3)\
**Post date:** [September 24, 2021, 11:12am UTC](https://discuss.hashicorp.com/t/importing-terraform-show-json-file-to-python/5793/3 "2021-09-24T11:12:55Z")

</div>

You need to pipe the file into jq before it will become readable like any other json file, otherwise it is only in a format that terraform can read.

`terraform show -json tfplan | jq`

Then you should be able to read it with `json.load()`.
