Basic JSON Parser Example
Read JSON files and extract tokens
- SDK directory:
apps/basic/json_parser
- Zentri App Store:
ZENTRI-BAJSONPARSER
API Features Demonstrated
Platforms
This app works on:
- AMW004 - Zentri Mackerel evaluation board
- AMW106 - Zentri Moray evaluation board
- NXP-SCCK - NXP Secure Cloud Connectivity Kit
Requirements and Prerequisites
This app requires a ZentriOS device.
Description
This app demonstrates the JSON parsing utility API.
Usage Instructions
No interaction is required. The first part of the output is as follows:
> JSON Parser demo starting...
Parsing: json/example1.json
[Closed: 0]
> JSON file contents:
Object: 2
String: markers
Array: 3
Object: 16
String: point
Array: 2
Float: 40.266044
Float: -74.718479
String: homeTeam
String: Lawrence Library
String: awayTeam
String: LUGip
String: markerImage
String: images/red.png
String: information
String: Linux users group meets second Wednesday of each month.
String: fixture
String: Wednesday 7pm
String: capacity
String:
String: previousScore
String:
...
This corresponds to the first part of the json/example1.json
file:
{"markers": [
{
"point":[40.266044,-74.718479],
"homeTeam":"Lawrence Library",
"awayTeam":"LUGip",
"markerImage":"images/red.png",
"information": "Linux users group meets second Wednesday of each month.",
"fixture":"Wednesday 7pm",
"capacity":"",
"previousScore":""
},
...
] }
After completing the sequence of parsing steps, the app exits and the device reboots.
Implementation
parse_example1.c
The example source parse_example1.c is a basic JSON parsing example. It uses a statically allocated buffer for chunked reading of the JSON file.
- The call to json_parse_context_init passes a configuration that specifies the
file_reader
callback. - The call to json_parse_chunked parses the file with repeated calls to the
file_reader
callback - During parsing, each JSON token is dynamically allocated and added to the context linked-list.
- When parsing is complete, calls to json_context_get_token retrieve parsed tokens from the context linked-list.
- Tokens are analysed and the results logged on the ZentriOS terminal output.
- The context and tokens linked-list is de-allocated at the end.
parse_example2.c
The example source parse_example2.c demonstrates having the entire JSON string pre-read into a buffer. The json buffer is then parsed and the tokens are looked up by their string value.
parse_example3.c
The example source parse_example3.c demonstrates reading a JSON file in chunks and keeping only a subset of the tokens.
The call to json_parse_context_init passes a configuration specifying the file_reader
callback and a json_token_callback
function, called before a token is allocated. It tests the token for meeting certain criteria and sets values of the keys within the context structure. It returns ZOS_SUCCESS if the token is to be kept and ZOS_ABORTED if the token is to be discarded.
parse_example9.c
The example source parse_example9.c demonstrates reading nested values from a JSON structure.
Source
See:
See also: