|
| 1 | +--- |
| 2 | +pcx_content_type: how-to |
| 3 | +title: Capture structured data |
| 4 | +sidebar: |
| 5 | + order: 9 |
| 6 | +--- |
| 7 | + |
| 8 | +The `/json` endpoint extracts structured data from a webpage. You can specify the expected output using either a `prompt` or a `response_format` parameter which accepts a JSON schema. The endpoint returns the extracted data in JSON format. |
| 9 | + |
| 10 | +## Parameters |
| 11 | + |
| 12 | +| Parameter | Mandatory | Note | |
| 13 | +| --------------- | --------- | ---------------------------------------------------------------------------- | |
| 14 | +| URL | yes | The URL of the webpage to extract data from. | |
| 15 | +| prompt | no | Must supply one of `prompt` or `response_format`. | |
| 16 | +| response_format | no | Must supply one of `prompt` or `response_format`. May include a JSON schema. | |
| 17 | + |
| 18 | +:::note[Note] |
| 19 | + |
| 20 | +The `/json` endpoint leverages [Workers AI](/workers-ai/) for data extraction. Using this endpoint incurs usage on Workers AI, which you can monitor usage through the Workers AI Dashboard. |
| 21 | + |
| 22 | +::: |
| 23 | + |
| 24 | +## Basic Usage |
| 25 | + |
| 26 | +### With a Prompt and JSON schema |
| 27 | + |
| 28 | +This example captures webpage data by providing both a prompt and a JSON schema. The prompt guides the extraction process, while the JSON schema defines the expected structure of the output. |
| 29 | + |
| 30 | +```bash |
| 31 | +curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID/browser-rendering/json' \ |
| 32 | + --header 'authorization: Bearer CF_API_TOKEN' \ |
| 33 | + --header 'content-type: application/json' \ |
| 34 | + --data '{ |
| 35 | + "url": "https://developers.cloudflare.com/", |
| 36 | + "prompt": "Get me the list of AI products", |
| 37 | + "response_format": { |
| 38 | + "type": "json_schema", |
| 39 | + "json_schema": { |
| 40 | + "type": "object", |
| 41 | + "properties": { |
| 42 | + "products": { |
| 43 | + "type": "array", |
| 44 | + "items": { |
| 45 | + "type": "object", |
| 46 | + "properties": { |
| 47 | + "name": { |
| 48 | + "type": "string" |
| 49 | + }, |
| 50 | + "link": { |
| 51 | + "type": "string" |
| 52 | + } |
| 53 | + }, |
| 54 | + "required": [ |
| 55 | + "name" |
| 56 | + ] |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | +}' |
| 63 | +``` |
| 64 | + |
| 65 | + |
| 66 | +```json output collapse={15-48} |
| 67 | +{ |
| 68 | + "success": true, |
| 69 | + "result": { |
| 70 | + "products": [ |
| 71 | + { |
| 72 | + "name": "Build a RAG app", |
| 73 | + "link": "https://developers.cloudflare.com/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai/" |
| 74 | + }, |
| 75 | + { |
| 76 | + "name": "Workers AI", |
| 77 | + "link": "https://developers.cloudflare.com/workers-ai/" |
| 78 | + }, |
| 79 | + { |
| 80 | + "name": "Vectorize", |
| 81 | + "link": "https://developers.cloudflare.com/vectorize/" |
| 82 | + }, |
| 83 | + { |
| 84 | + "name": "AI Gateway", |
| 85 | + "link": "https://developers.cloudflare.com/ai-gateway/" |
| 86 | + }, |
| 87 | + { |
| 88 | + "name": "AI Playground", |
| 89 | + "link": "https://playground.ai.cloudflare.com/" |
| 90 | + } |
| 91 | + ] |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +### With only a prompt |
| 97 | + |
| 98 | +In this example, only a prompt is provided. The endpoint will use the prompt to extract the data, but the response will not be structured according to a JSON schema. |
| 99 | +This is useful for simple extractions where you don't need a specific format. |
| 100 | + |
| 101 | +```bash |
| 102 | +curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID/browser-rendering/json' \ |
| 103 | + --header 'authorization: Bearer CF_API_TOKEN' \ |
| 104 | + --header 'content-type: application/json' \ |
| 105 | + --data '{ |
| 106 | + "url": "https://developers.cloudflare.com/", |
| 107 | + "prompt": "get me the list of AI products" |
| 108 | + }' |
| 109 | +``` |
| 110 | + |
| 111 | + |
| 112 | +```json output |
| 113 | + |
| 114 | + "success": true, |
| 115 | + "result": { |
| 116 | + "AI Products": [ |
| 117 | + "Build a RAG app", |
| 118 | + "Workers AI", |
| 119 | + "Vectorize", |
| 120 | + "AI Gateway", |
| 121 | + "AI Playground" |
| 122 | + ] |
| 123 | + } |
| 124 | +} |
| 125 | +``` |
| 126 | + |
| 127 | +### With only a JSON schema (no prompt) |
| 128 | + |
| 129 | +In this case, you supply a JSON schema via the `response_format` parameter. The schema defines the structure of the extracted data. |
| 130 | + |
| 131 | +```bash |
| 132 | +curl --request POST 'https://api.cloudflare.com/client/v4/accounts/CF_ACCOUNT_ID/browser-rendering/json' \ |
| 133 | + --header 'authorization: Bearer CF_API_TOKEN' \ |
| 134 | + --header 'content-type: application/json' \ |
| 135 | + --data '"response_format": { |
| 136 | + "type": "json_schema", |
| 137 | + "json_schema": { |
| 138 | + "type": "object", |
| 139 | + "properties": { |
| 140 | + "products": { |
| 141 | + "type": "array", |
| 142 | + "items": { |
| 143 | + "type": "object", |
| 144 | + "properties": { |
| 145 | + "name": { |
| 146 | + "type": "string" |
| 147 | + }, |
| 148 | + "link": { |
| 149 | + "type": "string" |
| 150 | + } |
| 151 | + }, |
| 152 | + "required": [ |
| 153 | + "name" |
| 154 | + ] |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + }' |
| 160 | +``` |
| 161 | + |
| 162 | + |
| 163 | +```json output collapse={13-68} |
| 164 | +{ |
| 165 | + "success": true, |
| 166 | + "result": { |
| 167 | + "products": [ |
| 168 | + { |
| 169 | + "name": "Workers", |
| 170 | + "link": "https://developers.cloudflare.com/workers/" |
| 171 | + }, |
| 172 | + { |
| 173 | + "name": "Pages", |
| 174 | + "link": "https://developers.cloudflare.com/pages/" |
| 175 | + }, |
| 176 | + { |
| 177 | + "name": "R2", |
| 178 | + "link": "https://developers.cloudflare.com/r2/" |
| 179 | + }, |
| 180 | + { |
| 181 | + "name": "Images", |
| 182 | + "link": "https://developers.cloudflare.com/images/" |
| 183 | + }, |
| 184 | + { |
| 185 | + "name": "Stream", |
| 186 | + "link": "https://developers.cloudflare.com/stream/" |
| 187 | + }, |
| 188 | + { |
| 189 | + "name": "Build a RAG app", |
| 190 | + "link": "https://developers.cloudflare.com/workers-ai/tutorials/build-a-retrieval-augmented-generation-ai/" |
| 191 | + }, |
| 192 | + { |
| 193 | + "name": "Workers AI", |
| 194 | + "link": "https://developers.cloudflare.com/workers-ai/" |
| 195 | + }, |
| 196 | + { |
| 197 | + "name": "Vectorize", |
| 198 | + "link": "https://developers.cloudflare.com/vectorize/" |
| 199 | + }, |
| 200 | + { |
| 201 | + "name": "AI Gateway", |
| 202 | + "link": "https://developers.cloudflare.com/ai-gateway/" |
| 203 | + }, |
| 204 | + { |
| 205 | + "name": "AI Playground", |
| 206 | + "link": "https://playground.ai.cloudflare.com/" |
| 207 | + }, |
| 208 | + { |
| 209 | + "name": "Access", |
| 210 | + "link": "https://developers.cloudflare.com/cloudflare-one/policies/access/" |
| 211 | + }, |
| 212 | + { |
| 213 | + "name": "Tunnel", |
| 214 | + "link": "https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/" |
| 215 | + }, |
| 216 | + { |
| 217 | + "name": "Gateway", |
| 218 | + "link": "https://developers.cloudflare.com/cloudflare-one/policies/gateway/" |
| 219 | + }, |
| 220 | + { |
| 221 | + "name": "Browser Isolation", |
| 222 | + "link": "https://developers.cloudflare.com/cloudflare-one/policies/browser-isolation/" |
| 223 | + }, |
| 224 | + { |
| 225 | + "name": "Replace your VPN", |
| 226 | + "link": "https://developers.cloudflare.com/learning-paths/replace-vpn/concepts/" |
| 227 | + } |
| 228 | + ] |
| 229 | + } |
| 230 | +} |
| 231 | +``` |
0 commit comments