Skip to content

Commit 7de2d4a

Browse files
committed
added example files and started readme documentation
1 parent ea27c97 commit 7de2d4a

File tree

4 files changed

+3389
-1
lines changed

4 files changed

+3389
-1
lines changed

README.md

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,128 @@
1-
This is a little utility that will transform one JSON object structure to another structure defined by a template.
1+
2+
## About json2json
3+
4+
This is a little utility that will transform one JSON object structure to another structure defined by a template.
5+
json2json is ideal for consuming JSON from a web service and transforming it into the structure your application needs it in.
6+
7+
json2json is written in CoffeeScript, which is easily translated into JavaScript.
8+
It's designed to run in a Node.js environment with the CoffeeScript package installed.
9+
You can require json2json just like any other package.
10+
11+
## Template options
12+
13+
There are several template options for transforming JSON.
14+
The template describes what the resulting document structure will look like.
15+
16+
### Basic template structure
17+
18+
A template specifies a set of rules that describe how to transform a property in a JSON object.
19+
A template itself is a javascript object.
20+
This example is written in CoffeeScript and has in-line comments to explain each property:
21+
22+
# Define a variable to hold your template object.
23+
# When the JSON object is transformed, the root template will process the root of the
24+
# JSON object.
25+
tmpl =
26+
# The "path" property (required) describes what property to transform in the JSON object.
27+
# The '.' value specifies that the root of the JSON object should be transformed.
28+
# The JSON object could be an object or an array.
29+
path: '.'
30+
# The "aggregate" property (optional) allows you to combine array values into one value.
31+
# (Think of it as the "reduce" part of a map/reduce.)
32+
# Each property of the "aggregate" object specifies a function that handles each value in an array.
33+
# You can manipulate the "existing" value (anything that has already been aggregated)
34+
# or return a new value to use for the "items" property on your new JSON object.
35+
# (You can specify as many )
36+
aggregate:
37+
items: (key, value, existing) -> if !isArray(value) then value else value[0]
38+
# The "as" property allows you to
39+
as:
40+
bins:
41+
path: 'Items.SearchBinSets.SearchBinSet.Bin'
42+
key: 'BinParameter.Value'
43+
value: 'BinItemCount'
44+
aggregate: (key, value, existing) -> Math.max(value, existing || 0)
45+
items:
46+
path: 'Items.Item'
47+
all: true
48+
as:
49+
#ASIN: 'ASIN'
50+
rank: 'SalesRank'
51+
title: 'ItemAttributes.Title'
52+
artist: 'ItemAttributes.Artist'
53+
manufacturer: 'ItemAttributes.Manufacturer'
54+
category: 'ItemAttributes.ProductGroup'
55+
price: 'Offers.Offer.OfferListing.Price.FormattedPrice'
56+
percent_saved: 'Offers.Offer.OfferListing.PercentageSaved'
57+
availability: 'Offers.Offer.OfferListing.Availability'
58+
price_new: 'OfferSummary.LowestNewPrice.FormattedPrice'
59+
price_used: 'OfferSummary.LowestUsedPrice.FormattedPrice'
60+
url: 'DetailPageURL'
61+
similar:
62+
path: 'SimilarProducts.SimilarProduct'
63+
key: 'ASIN'
64+
value: 'Title'
65+
66+
67+
68+
tmpl =
69+
path: '.'
70+
#all: true
71+
aggregate:
72+
total: (key, value, existing) -> if !sysmo.isArray(value) then value else value.sort().reverse()[0]
73+
pages: (key, value, existing) -> if !sysmo.isArray(value) then value else value.sort().reverse()[0]
74+
as:
75+
bins:
76+
path: 'Items.SearchBinSets.SearchBinSet.Bin'
77+
key: 'BinParameter.Value'
78+
value: 'BinItemCount'
79+
aggregate: (key, value, existing) -> Math.max(value, existing || 0)
80+
items:
81+
path: 'Items.Item'
82+
all: true
83+
as:
84+
#ASIN: 'ASIN'
85+
rank: 'SalesRank'
86+
title: 'ItemAttributes.Title'
87+
artist: 'ItemAttributes.Artist'
88+
manufacturer: 'ItemAttributes.Manufacturer'
89+
category: 'ItemAttributes.ProductGroup'
90+
price: 'Offers.Offer.OfferListing.Price.FormattedPrice'
91+
percent_saved: 'Offers.Offer.OfferListing.PercentageSaved'
92+
availability: 'Offers.Offer.OfferListing.Availability'
93+
price_new: 'OfferSummary.LowestNewPrice.FormattedPrice'
94+
price_used: 'OfferSummary.LowestUsedPrice.FormattedPrice'
95+
url: 'DetailPageURL'
96+
similar:
97+
path: 'SimilarProducts.SimilarProduct'
98+
key: 'ASIN'
99+
value: 'Title'
100+
images:
101+
path: '.'
102+
choose: ['SmallImage', 'MediumImage', 'LargeImage']
103+
format: (node, value, key) -> key: key.replace(/Image$/, '').toLowerCase()
104+
nested: true
105+
as:
106+
url: 'URL'
107+
height: 'Height.#'
108+
width: 'Width.#'
109+
image_sets:
110+
path: 'ImageSets.ImageSet'
111+
key: '@.Category'
112+
choose: (node, value, key) -> key isnt '@'
113+
format: (node, value, key) -> key: key.replace(/Image$/, '').toLowerCase()
114+
nested: true
115+
as:
116+
url: 'URL'
117+
height: 'Height.#'
118+
width: 'Width.#'
119+
links:
120+
path: 'ItemLinks.ItemLink'
121+
key: 'Description'
122+
value: 'URL'
123+
124+
new ObjectTemplate(tmpl).transform data
125+
126+
## License
2127

3128
Created by Joel Van Horn. Free to use however you please.

0 commit comments

Comments
 (0)