Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
jsonata is a lightweight query and transformation language for JSON data. It allows you to query, transform, and manipulate JSON data with a simple and expressive syntax.
Querying JSON Data
This feature allows you to query JSON data using a simple and expressive syntax. In this example, we query for a person named 'John' in the 'people' array.
const jsonata = require('jsonata');
const data = { "people": [{ "name": "John", "age": 30 }, { "name": "Jane", "age": 25 }] };
const expression = jsonata('people[name="John"]');
const result = expression.evaluate(data);
console.log(result); // Output: { "name": "John", "age": 30 }
Transforming JSON Data
This feature allows you to transform JSON data into a new structure. In this example, we transform the 'people' array to have 'fullName' and 'yearsOld' properties instead of 'name' and 'age'.
const jsonata = require('jsonata');
const data = { "people": [{ "name": "John", "age": 30 }, { "name": "Jane", "age": 25 }] };
const expression = jsonata('people.{"fullName": name, "yearsOld": age}');
const result = expression.evaluate(data);
console.log(result); // Output: [{ "fullName": "John", "yearsOld": 30 }, { "fullName": "Jane", "yearsOld": 25 }]
Aggregating JSON Data
This feature allows you to perform aggregations on JSON data. In this example, we calculate the sum of ages in the 'people' array.
const jsonata = require('jsonata');
const data = { "people": [{ "name": "John", "age": 30 }, { "name": "Jane", "age": 25 }] };
const expression = jsonata('people.age.sum()');
const result = expression.evaluate(data);
console.log(result); // Output: 55
jq is a lightweight and flexible command-line JSON processor. It allows you to slice, filter, map, and transform structured data with the same ease that sed, awk, grep, and friends let you play with text. Compared to jsonata, jq is more suited for command-line usage and scripting.
lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides a wide range of utility functions for common programming tasks, including manipulating and querying JSON data. While lodash is more general-purpose, jsonata is specifically designed for querying and transforming JSON.
underscore is a JavaScript library that provides a whole mess of useful functional programming helpers without extending any built-in objects. It includes functions for working with arrays, objects, and other data structures, making it useful for manipulating JSON data. However, jsonata offers a more specialized and expressive syntax for JSON querying and transformation.
JSON query and transformation language
##Introduction The primary purpose of this language is to extract values from JSON documents, with the additional capabilities to combine these values using a set of basic functions and operators, and also the ability to format the output into any arbitrary JSON structure.
##Install
npm install jsonata
##Usage In node.js (works in v0.10 and later):
var jsonata = require("jsonata");
var data = { "example": [ {"value": 4}, {"value": 7}, {"value": 13}] };
var expression = "$sum(example.value)";
var result = jsonata(expression).evaluate(data); // returns 24
In a browser (works in latest Chrome, Firefox, Safari):
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSONata test</title>
<script src="lib/jsonata.js"></script>
</head>
<body>
<button onclick="alert(jsonata('[1..10]').evaluate())">Click me</button>
</body>
</html>
##Tutorial A tutorial on the JSONata language is available here
##Developers If you want to run the latest code from git, here's how to get started:
Clone the code:
git clone https://github.com/jsonata-js/jsonata.git
cd jsonata
Install the development dependencies (there are no runtime dependencies):
npm install
Run the tests
npm t
##Errors
If an expression throws an error, e.g. syntax error or a runtime error (type error), then the object thrown has a consistent structure containing the column number of the error, the token that caused the error, and any other relevant information, including a meaningful message string.
For example:
{ "position": 16, "token": "}", "value": "]", "message": "Syntax error: expected ']' got '}' at column 16" }
##More Information Tutorial tutorial.md Function library functions.md
See the CONTRIBUTING.md for details of how to contribute to this repo.
FAQs
JSON query and transformation language
The npm package jsonata receives a total of 0 weekly downloads. As such, jsonata popularity was classified as not popular.
We found that jsonata demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.