Giter Site home page Giter Site logo

magicianred / transformjsontojson Goto Github PK

View Code? Open in Web Editor NEW

This project forked from black-beard-sdk/transformjsontojson

2.0 0.0 0.0 82 KB

Manage configuration in json for transform a json source in json target

License: BSD 3-Clause "New" or "Revised" License

C# 100.00%

transformjsontojson's Introduction

Transform Json To Json

Manage configuration for transform a json source in json target.

Consider the following template. note that the template is a json that describe the structure of the target json. If the template is empty, the process return the initial source json.

    { "name" : "jpath:{$.n}" }

In this case. the result will be an object with a property named "name" and the value will be the property "n" at the root of the source json.

And now the document source

    { "n" : "name1" }

The result will be.

    { "name" : "name1" }

The template is a valid json structur. the value in string have a specific syntax.

    "key:{argument} key:{argument} ..."

the key is a name of the service you need to call. You are responsable of registered the services in the configuration for matching with the template. the unique key registered is jpath -> "jpath:{valid json path expression}". The json path fetch the value at specified adress in the source document. jpath as json path is a query language for JSON, similar to XPath for XML. The implementation of JsonPath is did by newtonsoft. SelectToken

// Template
{ "prices": "jpath:{$..n} | sum:{}" } // service sum is before registered in the services list.

// Source
{ "prices": [{"n" : 1}, {"n" : 2}, {"n" : 3}] }

// Result
{ "prices":  6 }

How to use

// Intialization of the configuration
var configuration = new TranformJsonAstConfiguration();
TemplateTransformProvider Templateprovider = new TemplateTransformProvider(configuration);

// add a custom service
configuration.AddService("sum", () => new ServiceSum());

//Build the template translator
StringBuilder sbPayloadTemplate = new StringBuilder(@"payload template");
XjsltTemplate template = Templateprovider.GetTemplate(sbPayloadTemplate);

StringBuilder sbSource = new StringBuilder(@"payload source json");
JToken result = template.Transform(sbSource);

JSONPath Syntax

JSONPath notation

A JSONPath expression specifies a path to an element (or a set of elements) in a JSON structure. Paths can use the dot notation:

$.store.books[0].title

or the bracket notation:

$['store']['books'][0]['title']

The leading $ represents the root object or array and can be omitted. For example, $.foo.bar and foo.bar are the same, and so are $[0].status and [0].status.

Other syntax elements are described below.

Expression Description
$ The root object or array
.property Selects the specified property in a parent object.
['property'] Selects the specified property in a parent object. Be sure to put single quotes around the property name. Tip: Use this notation if the property name contains special characters such as spaces, or begins with a character other than A..Za..z_.
[n] Selects the n-th element from an array. Indexes are 0-based.
[index1,index2,โ€ฆ] Selects array elements with the specified indexes.
.._property_ Recursive descent: Searches for the specified property name recursively and returns an array of all values with this property name. Always returns a list even if just one property is found.
* Wildcard selects all elements in an object or an array, regardless of their names or indexes. For example, address.* means all properties of the address object, and book[*] means all items of the book array.
[_start_:_end_] [_start_:] Selects array elements from the start index and up to, but not including, end index. If end is omitted, selects all elements from start until the end of the array. Returns a list
[:n] Selects the first n elements of the array. Returns a list
[_-n_:] Selects the last n elements of the array. Returns a list
[?(_expression_)] Selects all elements in an object or array that match the specified filter. Returns a list
[(_expression_)] Script expressions can be used instead of explicit property names or indexes. An example is [(@.length-1)] which selects the last item in an array. Here, length refers to the length of the current array rather than a JSON field named length.
@ Used in filter expressions to refer to the current node being processed.

Notes:

  • Json path expressions, including property names and values, are case-sensitive.
  • Json path does not have operations for accessing parent or sibling nodes from the given node.

Filters

Filters are logical expressions used to filter arrays. An example of a JSONPath expression with a filter is

$.store.book[?(@.price < 10)]

where @ represents the current array item or object being processed. Filters can also use $ to refer to the properties outside of the current object:

$.store.book[?(@.price < $.expensive)]

An expression that specifies just a property name, such as [?(@.isbn)], matches all items that have this property, regardless of the value.

Below are the operators that can be used in filters.

Operator Description
== Equals to. String values must be enclosed in single quotes (not double quotes): [?(@.color=='red')].
!= Not equal to. String values must be enclosed in single quotes: [?(@.color!='red')].
> Greater than.
>= Greater than or equal to.
< Less than.
<= Less than or equal to.
=~ Matches a JavaScript regular expression. For example, [?(@.description =~ /cat.*/i)] matches items whose description starts with cat (case-insensitive).
! Used to negate a filter: [?([email protected])] matches items that do not have the isbn property.
&& Logical AND, used to combine multiple filter expressions: [?(@.category=='fiction' && @.price < 10)]
`
in Checks if the left-side value is present in the right-side list. Similar to the SQL IN operator. String comparison is case-sensitive.[?(@.size in ['M', 'L'])] [?('S' in @.sizes)]
nin Opposite of in. Checks that the left-side value is not present in the right-side list. String comparison is case-sensitive.[?(@.size nin ['M', 'L'])] [?('S' nin @.sizes)]
subsetof Checks if the left-side array is a subset of the right-side array. The actual order of array items does not matter. String comparison is case-sensitive. An empty left-side array always matches. For example:- [?(@.sizes subsetof ['M', 'L'])] โ€“ matches if sizes is ['M'] or ['L'] or ['L', 'M'] but does not match if the array has any other elements. [?(['M', 'L'] subsetof @.sizes)] matches if sizes contains at least 'M' and 'L'.
contains Checks if a string contains the specified substring (case-sensitive), or an array contains the specified element. [?(@.name contains 'Alex')] [?(@.numbers contains 7)] [?('ABCDEF' contains @.character)]
size Checks if an array or string has the specified length. [?(@.name size 4)]
empty true Matches an empty array or string. [?(@.name empty true)]
empty false Matches a non-empty array or string. [?(@.name empty false)]

Examples

For these examples, we will use a modified version of JSON from http://goessner.net/articles/JsonPath/index.html#e3:

{  
"store": {  
	"book": [  
	{  
		"category": "reference",  
		"author": "Nigel Rees",  
		"title": "Sayings of the Century",  
		"price": 8.95  
	},  
	{  
		"category": "fiction",  
		"author": "Herman Melville",  
		"title": "Moby Dick",  
		"isbn": "0-553-21311-3",  
		"price": 8.99  
	},  
	{  
		"category": "fiction",  
		"author": "J.R.R. Tolkien",  
		"title": "The Lord of the Rings",  
		"isbn": "0-395-19395-8",  
		"price": 22.99  
	}  
	],  
		"bicycle": {  
		"color": "red",  
		"price": 19.95  
	}  
},  
"expensive": 10  
}

In all these examples, the leading $. is optional and can be omitted.

Expression Meaning
$.store.* All direct properties of store (not recursive).
$.store.bicycle.color The color of the bicycle in the store.Result: red
$.store..price $..price The prices of all items in the store. Result: [8.95, 8.99, 22.99, 19.95]
$.store.book[*] $..book[*] All books in the store.
$..book[*].title The titles of all books in the store. Result: [Sayings of the Century, Moby Dick, The Lord of the Rings]
$..book[0] The first book.
$..book[0].title The title of the first book. Result: Sayings of the Century
$..book[0,1].title $..book[:2].title The titles of the first two books. Result: [Sayings of the Century, Moby Dick]
$..book[-1:].title $..book[(@.length-1)].title The title of the last book. Result: [The Lord of the Rings] The result is a list because [-n:] always returns lists.
$..book[?(@.author=='J.R.R. Tolkien')].title The titles of all books by J.R.R. Tolkien (exact match, case-sensitive). Result: [The Lord of the Rings] The result is a list, because filters always return lists.
$..book[?(@.isbn)] All books that have the isbn property.
$..book[?([email protected])] All books without the isbn property.
$..book[?(@.price < 10)] All books cheaper than 10.
$..book[?(@.price > $.expensive)] All expensive books.
$..book[?(@.author =~ /.*Tolkien/i)] All books whose author name ends with Tolkien (case-insensitive).
`$..book[?(@.category == 'fiction'
$..* All members of the JSON structure beneath the root (child objects, individual property values, array items), combined into an array.

transformjsontojson's People

Contributors

gaelgael5 avatar

Stargazers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.