Blog · Answer ·
How do I read SO4AG facts in JavaScript or Python?
Read a SO4AG source in JavaScript or Python and preserve each fact’s value, conditions, last day, source reference, and stated status.
Read the business’s so4ag.json file as JSON in JavaScript or Python. The SO4AG source repository provides readers for both languages. When you map the parsed data into your application, keep each fact’s value, conditions, last day of validity, source reference, and stated status together. Preserve not stated as its own status and treat a fact as history after its last day.
Find the readers and install the Python package
SO4AG (Source Objects for AGents) is an open format for business facts and a working draft at version 0.1. You can find both language readers and a validator in the SO4AG source repository.
Install the published Python package in your project environment:
pip install so4ag
For JavaScript, start with the reader in the repository. The repository also provides a command-line validator you can run against a source URL. Validation is a separate step from parsing. A JSON parser reads the structure of the document, while the SO4AG validator checks the document against the format.
Obtain and parse the source file
By convention, a business publishes its source, a JSON document containing its facts, at /so4ag.json on its domain. Fetch that address, check the HTTP response, and parse the body before passing the facts to the rest of your application.
Example: Example Rental publishes its source at https://example-rental.example/so4ag.json. Replace that example address with the source you want to read. These snippets show the built-in JSON interfaces in each language. See the repository for the SO4AG reader interfaces.
const url = "https://example-rental.example/so4ag.json";
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Could not fetch SO4AG source (${response.status})`);
}
const source = await response.json();
import json
from urllib.request import urlopen
url = "https://example-rental.example/so4ag.json"
with urlopen(url) as response:
source = json.load(response)
Both snippets leave the parsed document in source. JavaScript checks the HTTP status explicitly. Python’s urlopen raises an error for an unsuccessful HTTP response. Handle fetch and parse failures at the point where your application loads the source. A failed load says nothing about whether the business states any individual fact.
Read the parts of a parsed fact
Use the profile to identify the facts you can read. A profile defines which facts exist for a type of business. Use the SO4AG 0.1 specification for the JSON field names and structure when mapping each parsed fact into your application.
| Part of the fact | What your application should retain |
|---|---|
| Value | The value and any qualifier it carries. A price marked as a starting amount should display as a starting amount. |
| Conditions | The scope of the value, such as a country, customer type, item, or branch. The condition value any explicitly covers every value for that condition. |
| Last day of validity | The final day the fact applies. Keep the day with the value so application code can tell when the fact becomes history. |
| Source reference | The address of the page where the business states the fact, plus the exact words when supplied. |
| Stated status | Whether the business states the fact. Preserve not stated when the source uses that status. |
For Example Rental, a price scoped to customer: business applies only to business customers. Keep the customer condition attached when you store or display the price. If the price also carries a starting-amount qualifier, keep that qualifier through to the display.
Preserve unstated and historical facts
Keep not stated distinct from a stated value and evaluate the last day separately. A program has less room for error when it can see both whether a value was stated and when the fact applies.
Map not stated to an explicit status in code and display it as “Not stated” when the reader needs to see it. Reserve zero for an actual stated zero, and keep an empty field or an unlimited value distinct from the status. This matters for facts such as a mileage limit or refundable deposit, where substituting a default changes what the application tells the reader.
The last day is inclusive. Use the fact as current through that day, subject to its conditions. Once the current date is later than the last day, exclude the fact from current results and retain it as history. For more detail, read the article on what happens when a SO4AG fact reaches its last day.
Common questions
Do I need an account to read a source?
SO4AG is free to use and needs no account. You can fetch the JSON file directly when the business publishes it at a public address.
Can I read the same facts from a page?
Yes. A page can carry the same facts as JSON-LD, a way to embed structured data. Converting between the source file and JSON-LD preserves all the information.
Can I assume every source uses the same profile?
Read each source according to its profile. SO4AG includes General, Retail, and Rental profiles, and anyone can publish a profile of their own.
Next step
Open the SOQVER developer guide and load a source in your application. Check that a scoped value, a not stated fact, and a fact past its last day each reach your application with the information needed to display them correctly.