json.mdx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. ---
  2. title: '📃 JSON'
  3. ---
  4. To add any json file, use the data_type as `json`. Headers are included for each line, so if you have an `age` column, `18` will be added as `age: 18`. Eg:
  5. Here are the supported sources for loading `json`:
  6. ```
  7. 1. URL - valid url to json file that ends with ".json" extension.
  8. 2. Local file - valid url to local json file that ends with ".json" extension.
  9. 3. String - valid json string (e.g. - app.add('{"foo": "bar"}'))
  10. ```
  11. If you would like to add other data structures (e.x. list, dict etc.), do:
  12. ```python
  13. import json
  14. a = {"foo": "bar"}
  15. valid_json_string_data = json.dumps(a, indent=0)
  16. b = [{"foo": "bar"}]
  17. valid_json_string_data = json.dumps(b, indent=0)
  18. ```
  19. Example:
  20. ```python
  21. import os
  22. from embedchain.apps.app import App
  23. os.environ["OPENAI_API_KEY"] = "openai_api_key"
  24. app = App()
  25. response = app.query("What is the net worth of Elon Musk as of October 2023?")
  26. print(response)
  27. "I'm sorry, but I don't have access to real-time information or future predictions. Therefore, I don't know the net worth of Elon Musk as of October 2023."
  28. source_id = app.add("temp.json")
  29. response = app.query("What is the net worth of Elon Musk as of October 2023?")
  30. print(response)
  31. "As of October 2023, Elon Musk's net worth is $255.2 billion."
  32. ```
  33. temp.json
  34. ```json
  35. {
  36. "question": "What is your net worth, Elon Musk?",
  37. "answer": "As of October 2023, Elon Musk's net worth is $255.2 billion, making him one of the wealthiest individuals in the world."
  38. }
  39. ```