Hi All, I am trying to use from_json and to_json m...
# flyte-support
s
Hi All, I am trying to use from_json and to_json methods of FlyteFile to work. I am getting an error :
TypeError: FlyteFile.__init__() missing 1 required positional argument: 'path'
Code -
ff_uploaded = FlyteFile(full_s3_path).from_json(json_data)
Can you help me to understand the use of these methods. I appreciate your help in advance
d
hi
I want to know why
FlyteFile(full_s3_path)
has the method
from_json
s
d
I think you can read it directly
and it will be json file
let me try 1 now
Copy code
# FlyteFile("<s3://my-s3-bucket/s3_flyte_dir/test.json>")
from flytekit import task, workflow
from flytekit.types.file import FlyteFile

@task
def create_ff() -> FlyteFile:
    full_s3_path = "<s3://my-s3-bucket/test.json>"
    ff_uploaded = FlyteFile(full_s3_path)
    return ff_uploaded
@task
def read_ff(ff: FlyteFile):
    with open(ff, "r") as f:
        print(f.read())

@workflow
def wf():
    ff = create_ff()
    read_ff(ff=ff)


if __name__ == "__main__":
    wf()
hi @shy-needle-81848 can you try this too?
this work for me
Copy code
/Users/future-outlier/miniconda3/envs/dev/bin/python /Users/future-outlier/code/dev/flytekit/build/dataclass_remote_api/json_example.py 
21:40:04.824460 INFO     file.py:252 - Using flytectl/YAML config               
                         /Users/future-outlier/.flyte/config-sandbox.yaml       
21:40:05.927694 INFO     utils.py:344 - AsyncTranslate literal to python value. 
                         [Time: 0.000002s]                                      
21:40:05.929111 INFO     utils.py:344 - Translate literal to python value.      
                         [Time: 0.001436s]                                      
21:40:05.929437 INFO     base_task.py:751 - Invoking json_example.create_ff with
                         inputs: {}                                             
21:40:05.929748 INFO     utils.py:344 - Execute user level code. [Time:         
                         0.000005s]                                             
21:40:05.930230 INFO     utils.py:344 - Translate the output to literals. [Time:
                         0.000136s]                                             
21:40:05.930558 INFO     utils.py:344 - dispatch execute. [Time: 0.000513s]     
21:40:05.931275 INFO     utils.py:344 - AsyncTranslate literal to python value. 
                         [Time: 0.000001s]                                      
21:40:05.931707 INFO     utils.py:344 - Translate literal to python value.      
                         [Time: 0.000439s]                                      
21:40:05.932003 INFO     base_task.py:751 - Invoking json_example.read_ff with  
                         inputs: {'ff':                                         
                         /var/folders/3z/bsm4cddd4q18b_jc_2b2my4h0000gn/T/flyte-
                         2n5ewe9c/sandbox/local_flytekit/02dc0ddab7f02d4e2dba409
                         d0d639b43/test.json}                                   
21:40:05.933685 INFO     data_persistence.py:308 - Getting                      
                         <s3://my-s3-bucket/test.json> to                         
                         /var/folders/3z/bsm4cddd4q18b_jc_2b2my4h0000gn/T/flyte-
                         2n5ewe9c/sandbox/local_flytekit/02dc0ddab7f02d4e2dba409
                         d0d639b43/test.json                                    
21:40:06.003664 INFO     utils.py:344 - Download data to local from             
                         <s3://my-s3-bucket/test.json>. [Time: 0.071184s]         
{
    "glossary": {
        "title": "example glossary",
		"GlossDiv": {
            "title": "S",
			"GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
					"SortAs": "SGML",
					"GlossTerm": "Standard Generalized Markup Language",
					"Acronym": "SGML",
					"Abbrev": "ISO 8879:1986",
					"GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
						"GlossSeeAlso": ["GML", "XML"]
                    },
					"GlossSee": "markup"
                }
            }
        }
    }
}

21:40:06.004358 INFO     utils.py:344 - Execute user level code. [Time:         
                         0.071975s]                                             
21:40:06.004840 INFO     utils.py:344 - Translate the output to literals. [Time:
                         0.000006s]                                             
21:40:06.005226 INFO     utils.py:344 - dispatch execute. [Time: 0.000455s]     

Process finished with exit code 0
image.png
s
Hey @damp-lion-88352 Thank you very much for the detailed response. I am able to read the JSON using the above code. I am just wondering what does the from_json and to_json is used for.
d
okok
so Flytefile inherit
DataClassJSONMixin
this is
DataClassJSONMixin
's api
make flytefile, from a dataclass to a json str
s
ok. got it. Thank you :)