@terzogenito/json-utils 1.0.12 → 1.0.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +438 -438
- package/index.js +803 -669
- package/package.json +35 -35
package/README.md
CHANGED
|
@@ -1,439 +1,439 @@
|
|
|
1
|
-
# Documentation
|
|
2
|
-
|
|
3
|
-
## Overview
|
|
4
|
-
This module provides various functions for reading, validating, and processing JSON files and URL content.
|
|
5
|
-
|
|
6
|
-
## Installation
|
|
7
|
-
```bash
|
|
8
|
-
npm install @terzogenito/json-utils
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Quick Start
|
|
12
|
-
```javascript
|
|
13
|
-
import jsonUtils from '@terzogenito/json-utils';
|
|
14
|
-
|
|
15
|
-
const jsonAttributes = jsonUtils.getAttributes(jsonData);
|
|
16
|
-
console.log(jsonAttributes);
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Function List
|
|
20
|
-
|
|
21
|
-
### 1. `getData(path)`
|
|
22
|
-
```javascript
|
|
23
|
-
console.log(await app.getData('./test-data.json'));
|
|
24
|
-
```
|
|
25
|
-
|
|
26
|
-
**Description**: Reads and returns data from a file asynchronously.
|
|
27
|
-
|
|
28
|
-
**Parameters**:
|
|
29
|
-
- `path` (String): Path to the file to be read
|
|
30
|
-
|
|
31
|
-
---
|
|
32
|
-
|
|
33
|
-
### 2. `getString(path)`
|
|
34
|
-
```javascript
|
|
35
|
-
console.log(app.getString('test-data.json'));
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
**Description**: Reads a file and returns its content as a string.
|
|
39
|
-
|
|
40
|
-
**Parameters**:
|
|
41
|
-
- `path` (String): Path to the file to be read
|
|
42
|
-
|
|
43
|
-
---
|
|
44
|
-
|
|
45
|
-
### 3. `getFile(path, callback)`
|
|
46
|
-
```javascript
|
|
47
|
-
app.getFile('test-data.json', data => {
|
|
48
|
-
console.log(app.isValid(data));
|
|
49
|
-
});
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
**Description**: Reads a file and processes the data using a callback function.
|
|
53
|
-
|
|
54
|
-
**Parameters**:
|
|
55
|
-
- `path` (String): Path to the file to be read
|
|
56
|
-
- `callback` (Function): Function to be called with the file data
|
|
57
|
-
|
|
58
|
-
---
|
|
59
|
-
|
|
60
|
-
### 4. `isValid(data)`
|
|
61
|
-
```javascript
|
|
62
|
-
app.getFile('test-data.json', data => {
|
|
63
|
-
console.log(app.isValid(data));
|
|
64
|
-
});
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
**Description**: Validates whether the data is valid JSON.
|
|
68
|
-
|
|
69
|
-
**Parameters**:
|
|
70
|
-
- `data` (Any): Data to be validated
|
|
71
|
-
|
|
72
|
-
---
|
|
73
|
-
|
|
74
|
-
### 5. `toString(data)`
|
|
75
|
-
```javascript
|
|
76
|
-
app.getFile('test-data.json', data => {
|
|
77
|
-
console.log(app.toString(data));
|
|
78
|
-
});
|
|
79
|
-
```
|
|
80
|
-
|
|
81
|
-
**Description**: Converts JSON data to a string.
|
|
82
|
-
|
|
83
|
-
**Parameters**:
|
|
84
|
-
- `data` (Object): JSON data to be converted
|
|
85
|
-
|
|
86
|
-
---
|
|
87
|
-
|
|
88
|
-
### 6. `readJSON(jsonString)`
|
|
89
|
-
```javascript
|
|
90
|
-
console.log(app.readJSON('{"name":"John","age":30,"isActive":true}'));
|
|
91
|
-
```
|
|
92
|
-
|
|
93
|
-
**Description**: Reads and parses a JSON string into a JavaScript object.
|
|
94
|
-
|
|
95
|
-
**Parameters**:
|
|
96
|
-
- `jsonString` (String): JSON string to be parsed
|
|
97
|
-
|
|
98
|
-
---
|
|
99
|
-
|
|
100
|
-
### 7. `isJSON(jsonString)`
|
|
101
|
-
```javascript
|
|
102
|
-
console.log(app.isJSON('{"name":"John","age":30,"isActive":true}'));
|
|
103
|
-
```
|
|
104
|
-
|
|
105
|
-
**Description**: Checks if a string is valid JSON.
|
|
106
|
-
|
|
107
|
-
**Parameters**:
|
|
108
|
-
- `jsonString` (String): String to be checked
|
|
109
|
-
|
|
110
|
-
---
|
|
111
|
-
|
|
112
|
-
### 8. `isJSONObject(obj)`
|
|
113
|
-
```javascript
|
|
114
|
-
console.log(app.isJSONObject({"name": "John","age": 30,"isActive": true}));
|
|
115
|
-
```
|
|
116
|
-
|
|
117
|
-
**Description**: Checks if an object is a valid JSON object.
|
|
118
|
-
|
|
119
|
-
**Parameters**:
|
|
120
|
-
- `obj` (Object): Object to be checked
|
|
121
|
-
|
|
122
|
-
---
|
|
123
|
-
|
|
124
|
-
### 9. `beautifyJSON(jsonString, indent)`
|
|
125
|
-
```javascript
|
|
126
|
-
console.log(app.beautifyJSON(jsonString));
|
|
127
|
-
console.log(app.beautifyJSON(jsonString, 4));
|
|
128
|
-
```
|
|
129
|
-
|
|
130
|
-
**Description**: Formats a JSON string with customizable indentation.
|
|
131
|
-
|
|
132
|
-
**Parameters**:
|
|
133
|
-
- `jsonString` (String): JSON string to be formatted
|
|
134
|
-
- `indent` (Number, optional): Number of spaces for indentation (default: 2)
|
|
135
|
-
|
|
136
|
-
---
|
|
137
|
-
|
|
138
|
-
### 10. `beautify(jsonObject)`
|
|
139
|
-
```javascript
|
|
140
|
-
console.log(app.beautify(sampleJSONObject));
|
|
141
|
-
```
|
|
142
|
-
|
|
143
|
-
**Description**: Formats a JSON object into a readable string.
|
|
144
|
-
|
|
145
|
-
**Parameters**:
|
|
146
|
-
- `jsonObject` (Object): JSON object to be formatted
|
|
147
|
-
|
|
148
|
-
---
|
|
149
|
-
|
|
150
|
-
### 11. `getURL(url, callback)`
|
|
151
|
-
```javascript
|
|
152
|
-
app.getURL("https://example.com/data.txt", data => {
|
|
153
|
-
console.log(data);
|
|
154
|
-
});
|
|
155
|
-
```
|
|
156
|
-
|
|
157
|
-
**Description**: Fetches string content from a URL.
|
|
158
|
-
|
|
159
|
-
**Parameters**:
|
|
160
|
-
- `url` (String): Target URL
|
|
161
|
-
- `callback` (Function): Callback function that receives the data
|
|
162
|
-
|
|
163
|
-
---
|
|
164
|
-
|
|
165
|
-
### 12. `getJSON(url, callback)`
|
|
166
|
-
```javascript
|
|
167
|
-
app.getJSON("https://example.com/data.json", data => {
|
|
168
|
-
console.log(data);
|
|
169
|
-
});
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
**Description**: Fetches and parses JSON content from a URL.
|
|
173
|
-
|
|
174
|
-
**Parameters**:
|
|
175
|
-
- `url` (String): URL pointing to JSON file
|
|
176
|
-
- `callback` (Function): Callback function that receives JSON data
|
|
177
|
-
|
|
178
|
-
## Usage Examples
|
|
179
|
-
|
|
180
|
-
```javascript
|
|
181
|
-
const app = require('./index');
|
|
182
|
-
|
|
183
|
-
// Reading local file
|
|
184
|
-
const data = await app.getData('./data.json');
|
|
185
|
-
|
|
186
|
-
// Validating JSON
|
|
187
|
-
const isValid = app.isJSON('{"key": "value"}');
|
|
188
|
-
|
|
189
|
-
// Formatting JSON
|
|
190
|
-
const beautified = app.beautifyJSON('{"key":"value"}', 4);
|
|
191
|
-
|
|
192
|
-
// Fetching data from URL
|
|
193
|
-
app.getJSON("https://api.example.com/data", jsonData => {
|
|
194
|
-
console.log(jsonData);
|
|
195
|
-
});
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
### 13. getAttributes(jsonObject)
|
|
199
|
-
```javascript
|
|
200
|
-
const dataJSON = app.readJSON(data);
|
|
201
|
-
const attributes = app.getAttributes(dataJSON);
|
|
202
|
-
// Output: ['name', 'age', 'isActive', 'address', 'hobbies']
|
|
203
|
-
```
|
|
204
|
-
|
|
205
|
-
**Description**: Extracts all attribute/keys from a JSON object and returns them as an array.
|
|
206
|
-
|
|
207
|
-
**Parameters**:
|
|
208
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
209
|
-
|
|
210
|
-
Returns: Array of attribute names
|
|
211
|
-
|
|
212
|
-
### 14. getMeta(jsonObject)
|
|
213
|
-
```javascript
|
|
214
|
-
const meta = app.getMeta(dataJSON);
|
|
215
|
-
// Output: {"name": "string", "age": "integer", "isActive": "boolean"}
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
**Description**: Generates metadata showing attribute names and their data types.
|
|
219
|
-
|
|
220
|
-
**Parameters**:
|
|
221
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
222
|
-
|
|
223
|
-
Returns: Object with attribute names as keys and data types as values
|
|
224
|
-
|
|
225
|
-
Data Types Identified:
|
|
226
|
-
- string, integer, float, boolean, array, object, null, date
|
|
227
|
-
|
|
228
|
-
### 15. getMetaDetail(jsonObject)
|
|
229
|
-
```javascript
|
|
230
|
-
const metaDetail = app.getMetaDetail(dataJSON);
|
|
231
|
-
// Output: Detailed metadata including nested structures
|
|
232
|
-
```
|
|
233
|
-
|
|
234
|
-
**Description**: Generates comprehensive metadata including nested attributes, data types, paths, and structural information.
|
|
235
|
-
|
|
236
|
-
**Parameters**:
|
|
237
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
238
|
-
|
|
239
|
-
Returns: Object with detailed metadata for each attribute
|
|
240
|
-
|
|
241
|
-
Metadata Includes:
|
|
242
|
-
- type: Data type
|
|
243
|
-
- isRequired: Whether attribute exists
|
|
244
|
-
- path: Full path to attribute
|
|
245
|
-
- children: Nested attributes (for objects)
|
|
246
|
-
- length: Array length (for arrays)
|
|
247
|
-
- elementType: Type of array elements
|
|
248
|
-
|
|
249
|
-
### 16. getMetaCompact(jsonObject)
|
|
250
|
-
```javascript
|
|
251
|
-
const metaCompact = app.getMetaCompact(dataJSON);
|
|
252
|
-
// Output: Compact metadata format
|
|
253
|
-
```
|
|
254
|
-
|
|
255
|
-
**Description**: Creates a compact metadata representation showing the structure hierarchy.
|
|
256
|
-
|
|
257
|
-
**Parameters**:
|
|
258
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
259
|
-
|
|
260
|
-
Returns: Compact representation of JSON structure
|
|
261
|
-
|
|
262
|
-
Format Examples:
|
|
263
|
-
- "array[string]": Array of strings
|
|
264
|
-
- "array[object]": Array of objects
|
|
265
|
-
- Nested objects shown as nested objects
|
|
266
|
-
|
|
267
|
-
### 17. getPartial(jsonObject, attributes)
|
|
268
|
-
```javascript
|
|
269
|
-
const partial = app.getPartial(dataJSON, ["name", "age"]);
|
|
270
|
-
// Output: {"name": "John", "age": 30}
|
|
271
|
-
```
|
|
272
|
-
|
|
273
|
-
**Description**: Extracts specific attributes from a JSON object.
|
|
274
|
-
|
|
275
|
-
**Parameters**:
|
|
276
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
277
|
-
- attributes (Array|String|Object): Attributes to extract
|
|
278
|
-
|
|
279
|
-
attribute Parameter Types:
|
|
280
|
-
- Array: List of attribute names to extract
|
|
281
|
-
```javascript
|
|
282
|
-
app.getPartial(data, ["name", "age"])
|
|
283
|
-
```
|
|
284
|
-
- String: Single attribute name
|
|
285
|
-
```javascript
|
|
286
|
-
app.getPartial(data, "name")
|
|
287
|
-
```
|
|
288
|
-
- Object: Mapping of new names to original attributes
|
|
289
|
-
```javascript
|
|
290
|
-
app.getPartial(data, {"fullName": "name", "yearsOld": "age"})
|
|
291
|
-
```
|
|
292
|
-
|
|
293
|
-
Returns: Object containing only the specified attributes
|
|
294
|
-
|
|
295
|
-
### 18. getPartialDeep(jsonObject, attributePaths)
|
|
296
|
-
```javascript
|
|
297
|
-
const deepPartial = app.getPartialDeep(dataJSON, ["name", "address.city", "hobbies.length"]);
|
|
298
|
-
// Output: {"name": "John", "city": "Jakarta", "length": 2}
|
|
299
|
-
```
|
|
300
|
-
|
|
301
|
-
**Description**: Extracts attributes using nested paths (dot notation).
|
|
302
|
-
|
|
303
|
-
**Parameters**:
|
|
304
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
305
|
-
- attributePaths (Array|Object): Paths to extract
|
|
306
|
-
|
|
307
|
-
attributePaths Parameter Types:
|
|
308
|
-
- Array: List of dot-notated paths
|
|
309
|
-
```javascript
|
|
310
|
-
app.getPartialDeep(data, ["user.profile.name", "user.contact.email"])
|
|
311
|
-
```
|
|
312
|
-
- Object: Mapping of new names to paths
|
|
313
|
-
```javascript
|
|
314
|
-
app.getPartialDeep(data, {"userName": "user.profile.name", "userEmail": "user.contact.email"})
|
|
315
|
-
```
|
|
316
|
-
|
|
317
|
-
Returns: Object with extracted values (uses last path segment as key for array input)
|
|
318
|
-
|
|
319
|
-
### 19. getPartialWithDefaults(jsonObject, attributesConfig)
|
|
320
|
-
```javascript
|
|
321
|
-
const partialWithDefaults = app.getPartialWithDefaults(dataJSON, {
|
|
322
|
-
"name": "name",
|
|
323
|
-
"status": {
|
|
324
|
-
path: "isActive",
|
|
325
|
-
transform: (val) => val ? "Active" : "Inactive"
|
|
326
|
-
},
|
|
327
|
-
"email": {
|
|
328
|
-
path: "contact.email",
|
|
329
|
-
default: "no-email@example.com"
|
|
330
|
-
}
|
|
331
|
-
});
|
|
332
|
-
```
|
|
333
|
-
|
|
334
|
-
**Description**: Extracts attributes with advanced configuration including default values and transformations.
|
|
335
|
-
|
|
336
|
-
**Parameters**:
|
|
337
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
338
|
-
- attributesConfig (Object): Configuration object
|
|
339
|
-
|
|
340
|
-
Configuration Options:
|
|
341
|
-
- String: Simple path extraction
|
|
342
|
-
```javascript
|
|
343
|
-
"name": "user.fullName"
|
|
344
|
-
```
|
|
345
|
-
- Object: Advanced configuration
|
|
346
|
-
```javascript
|
|
347
|
-
"formattedAge": {
|
|
348
|
-
path: "age", // Required: Path to attribute
|
|
349
|
-
default: 0, // Optional: Default value if path doesn't exist
|
|
350
|
-
transform: (val) => ${val} years old // Optional: Transformation function
|
|
351
|
-
}
|
|
352
|
-
```
|
|
353
|
-
|
|
354
|
-
Returns: Object with extracted and processed values
|
|
355
|
-
|
|
356
|
-
### 20. excludeAttributes(jsonObject, attributesToExclude)
|
|
357
|
-
```javascript
|
|
358
|
-
const filtered = app.excludeAttributes(dataJSON, ["isActive", "address"]);
|
|
359
|
-
// Output: {"name": "John", "age": 30, "hobbies": ["reading", "coding"]}
|
|
360
|
-
```
|
|
361
|
-
|
|
362
|
-
**Description**: Creates a new JSON object excluding specified attributes.
|
|
363
|
-
|
|
364
|
-
**Parameters**:
|
|
365
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
366
|
-
- attributesToExclude (Array|String): Attributes to remove
|
|
367
|
-
|
|
368
|
-
attributesToExclude Parameter Types:
|
|
369
|
-
- Array: List of attribute names to exclude
|
|
370
|
-
```javascript
|
|
371
|
-
app.excludeAttributes(data, ["password", "secretKey"])
|
|
372
|
-
```
|
|
373
|
-
- String: Single attribute name to exclude
|
|
374
|
-
```javascript
|
|
375
|
-
app.excludeAttributes(data, "password")
|
|
376
|
-
```
|
|
377
|
-
|
|
378
|
-
Returns: New object without the excluded attributes
|
|
379
|
-
|
|
380
|
-
### 21. getAttributeValue(jsonObject, attributeName, defaultValue)
|
|
381
|
-
```javascript
|
|
382
|
-
const name = app.getAttributeValue(dataJSON, "name");
|
|
383
|
-
const email = app.getAttributeValue(dataJSON, "email", "default@email.com");
|
|
384
|
-
```
|
|
385
|
-
|
|
386
|
-
**Description**: Gets the value of a specific attribute with optional default value.
|
|
387
|
-
|
|
388
|
-
**Parameters**:
|
|
389
|
-
- jsonObject (Object|String): JSON object or JSON string
|
|
390
|
-
- attributeName (String): Name of the attribute to retrieve
|
|
391
|
-
- defaultValue (Any, optional): Default value if attribute doesn't exist
|
|
392
|
-
|
|
393
|
-
Returns: Attribute value or default value
|
|
394
|
-
|
|
395
|
-
Advanced Usage Examples
|
|
396
|
-
```javascript
|
|
397
|
-
const app = require('./index');
|
|
398
|
-
|
|
399
|
-
// Get all attributes from JSON
|
|
400
|
-
const data = await app.getData('./data.json');
|
|
401
|
-
const jsonData = app.readJSON(data);
|
|
402
|
-
const attributes = app.getAttributes(jsonData);
|
|
403
|
-
|
|
404
|
-
// Get metadata information
|
|
405
|
-
const meta = app.getMeta(jsonData);
|
|
406
|
-
const metaDetail = app.getMetaDetail(jsonData);
|
|
407
|
-
|
|
408
|
-
// Extract specific data
|
|
409
|
-
const userInfo = app.getPartial(jsonData, ["name", "email", "phone"]);
|
|
410
|
-
const nestedData = app.getPartialDeep(jsonData, ["user.profile.name", "user.contact.email"]);
|
|
411
|
-
|
|
412
|
-
// Extract with transformations and defaults
|
|
413
|
-
const processedData = app.getPartialWithDefaults(jsonData, {
|
|
414
|
-
"fullName": "user.name",
|
|
415
|
-
"ageFormatted": {
|
|
416
|
-
path: "user.age",
|
|
417
|
-
transform: (age) => ${age} years old
|
|
418
|
-
},
|
|
419
|
-
"country": {
|
|
420
|
-
path: "user.address.country",
|
|
421
|
-
default: "Unknown"
|
|
422
|
-
}
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
// Exclude sensitive information
|
|
426
|
-
const safeData = app.excludeAttributes(jsonData, ["password", "ssn", "creditCard"]);
|
|
427
|
-
|
|
428
|
-
// Analyze complex nested structures
|
|
429
|
-
const complexMeta = app.getMetaDetail(complexJSON);
|
|
430
|
-
console.log(complexMeta.user?.children?.contact?.children?.email?.type); // "string"
|
|
431
|
-
|
|
432
|
-
// Get single attribute value
|
|
433
|
-
const userName = app.getAttributeValue(jsonData, "name");
|
|
434
|
-
const userEmail = app.getAttributeValue(jsonData, "email", "no-email@example.com");
|
|
435
|
-
```
|
|
436
|
-
|
|
437
|
-
## Requirements
|
|
438
|
-
- Node.js 12 or higher
|
|
1
|
+
# Documentation
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
This module provides various functions for reading, validating, and processing JSON files and URL content.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
```bash
|
|
8
|
+
npm install @terzogenito/json-utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Quick Start
|
|
12
|
+
```javascript
|
|
13
|
+
import jsonUtils from '@terzogenito/json-utils';
|
|
14
|
+
|
|
15
|
+
const jsonAttributes = jsonUtils.getAttributes(jsonData);
|
|
16
|
+
console.log(jsonAttributes);
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Function List
|
|
20
|
+
|
|
21
|
+
### 1. `getData(path)`
|
|
22
|
+
```javascript
|
|
23
|
+
console.log(await app.getData('./test-data.json'));
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
**Description**: Reads and returns data from a file asynchronously.
|
|
27
|
+
|
|
28
|
+
**Parameters**:
|
|
29
|
+
- `path` (String): Path to the file to be read
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
### 2. `getString(path)`
|
|
34
|
+
```javascript
|
|
35
|
+
console.log(app.getString('test-data.json'));
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Description**: Reads a file and returns its content as a string.
|
|
39
|
+
|
|
40
|
+
**Parameters**:
|
|
41
|
+
- `path` (String): Path to the file to be read
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
### 3. `getFile(path, callback)`
|
|
46
|
+
```javascript
|
|
47
|
+
app.getFile('test-data.json', data => {
|
|
48
|
+
console.log(app.isValid(data));
|
|
49
|
+
});
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
**Description**: Reads a file and processes the data using a callback function.
|
|
53
|
+
|
|
54
|
+
**Parameters**:
|
|
55
|
+
- `path` (String): Path to the file to be read
|
|
56
|
+
- `callback` (Function): Function to be called with the file data
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
### 4. `isValid(data)`
|
|
61
|
+
```javascript
|
|
62
|
+
app.getFile('test-data.json', data => {
|
|
63
|
+
console.log(app.isValid(data));
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
**Description**: Validates whether the data is valid JSON.
|
|
68
|
+
|
|
69
|
+
**Parameters**:
|
|
70
|
+
- `data` (Any): Data to be validated
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
### 5. `toString(data)`
|
|
75
|
+
```javascript
|
|
76
|
+
app.getFile('test-data.json', data => {
|
|
77
|
+
console.log(app.toString(data));
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**Description**: Converts JSON data to a string.
|
|
82
|
+
|
|
83
|
+
**Parameters**:
|
|
84
|
+
- `data` (Object): JSON data to be converted
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
### 6. `readJSON(jsonString)`
|
|
89
|
+
```javascript
|
|
90
|
+
console.log(app.readJSON('{"name":"John","age":30,"isActive":true}'));
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
**Description**: Reads and parses a JSON string into a JavaScript object.
|
|
94
|
+
|
|
95
|
+
**Parameters**:
|
|
96
|
+
- `jsonString` (String): JSON string to be parsed
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
### 7. `isJSON(jsonString)`
|
|
101
|
+
```javascript
|
|
102
|
+
console.log(app.isJSON('{"name":"John","age":30,"isActive":true}'));
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
**Description**: Checks if a string is valid JSON.
|
|
106
|
+
|
|
107
|
+
**Parameters**:
|
|
108
|
+
- `jsonString` (String): String to be checked
|
|
109
|
+
|
|
110
|
+
---
|
|
111
|
+
|
|
112
|
+
### 8. `isJSONObject(obj)`
|
|
113
|
+
```javascript
|
|
114
|
+
console.log(app.isJSONObject({"name": "John","age": 30,"isActive": true}));
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**Description**: Checks if an object is a valid JSON object.
|
|
118
|
+
|
|
119
|
+
**Parameters**:
|
|
120
|
+
- `obj` (Object): Object to be checked
|
|
121
|
+
|
|
122
|
+
---
|
|
123
|
+
|
|
124
|
+
### 9. `beautifyJSON(jsonString, indent)`
|
|
125
|
+
```javascript
|
|
126
|
+
console.log(app.beautifyJSON(jsonString));
|
|
127
|
+
console.log(app.beautifyJSON(jsonString, 4));
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
**Description**: Formats a JSON string with customizable indentation.
|
|
131
|
+
|
|
132
|
+
**Parameters**:
|
|
133
|
+
- `jsonString` (String): JSON string to be formatted
|
|
134
|
+
- `indent` (Number, optional): Number of spaces for indentation (default: 2)
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
### 10. `beautify(jsonObject)`
|
|
139
|
+
```javascript
|
|
140
|
+
console.log(app.beautify(sampleJSONObject));
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
**Description**: Formats a JSON object into a readable string.
|
|
144
|
+
|
|
145
|
+
**Parameters**:
|
|
146
|
+
- `jsonObject` (Object): JSON object to be formatted
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
### 11. `getURL(url, callback)`
|
|
151
|
+
```javascript
|
|
152
|
+
app.getURL("https://example.com/data.txt", data => {
|
|
153
|
+
console.log(data);
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
**Description**: Fetches string content from a URL.
|
|
158
|
+
|
|
159
|
+
**Parameters**:
|
|
160
|
+
- `url` (String): Target URL
|
|
161
|
+
- `callback` (Function): Callback function that receives the data
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
### 12. `getJSON(url, callback)`
|
|
166
|
+
```javascript
|
|
167
|
+
app.getJSON("https://example.com/data.json", data => {
|
|
168
|
+
console.log(data);
|
|
169
|
+
});
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
**Description**: Fetches and parses JSON content from a URL.
|
|
173
|
+
|
|
174
|
+
**Parameters**:
|
|
175
|
+
- `url` (String): URL pointing to JSON file
|
|
176
|
+
- `callback` (Function): Callback function that receives JSON data
|
|
177
|
+
|
|
178
|
+
## Usage Examples
|
|
179
|
+
|
|
180
|
+
```javascript
|
|
181
|
+
const app = require('./index');
|
|
182
|
+
|
|
183
|
+
// Reading local file
|
|
184
|
+
const data = await app.getData('./data.json');
|
|
185
|
+
|
|
186
|
+
// Validating JSON
|
|
187
|
+
const isValid = app.isJSON('{"key": "value"}');
|
|
188
|
+
|
|
189
|
+
// Formatting JSON
|
|
190
|
+
const beautified = app.beautifyJSON('{"key":"value"}', 4);
|
|
191
|
+
|
|
192
|
+
// Fetching data from URL
|
|
193
|
+
app.getJSON("https://api.example.com/data", jsonData => {
|
|
194
|
+
console.log(jsonData);
|
|
195
|
+
});
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### 13. getAttributes(jsonObject)
|
|
199
|
+
```javascript
|
|
200
|
+
const dataJSON = app.readJSON(data);
|
|
201
|
+
const attributes = app.getAttributes(dataJSON);
|
|
202
|
+
// Output: ['name', 'age', 'isActive', 'address', 'hobbies']
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Description**: Extracts all attribute/keys from a JSON object and returns them as an array.
|
|
206
|
+
|
|
207
|
+
**Parameters**:
|
|
208
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
209
|
+
|
|
210
|
+
Returns: Array of attribute names
|
|
211
|
+
|
|
212
|
+
### 14. getMeta(jsonObject)
|
|
213
|
+
```javascript
|
|
214
|
+
const meta = app.getMeta(dataJSON);
|
|
215
|
+
// Output: {"name": "string", "age": "integer", "isActive": "boolean"}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
**Description**: Generates metadata showing attribute names and their data types.
|
|
219
|
+
|
|
220
|
+
**Parameters**:
|
|
221
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
222
|
+
|
|
223
|
+
Returns: Object with attribute names as keys and data types as values
|
|
224
|
+
|
|
225
|
+
Data Types Identified:
|
|
226
|
+
- string, integer, float, boolean, array, object, null, date
|
|
227
|
+
|
|
228
|
+
### 15. getMetaDetail(jsonObject)
|
|
229
|
+
```javascript
|
|
230
|
+
const metaDetail = app.getMetaDetail(dataJSON);
|
|
231
|
+
// Output: Detailed metadata including nested structures
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
**Description**: Generates comprehensive metadata including nested attributes, data types, paths, and structural information.
|
|
235
|
+
|
|
236
|
+
**Parameters**:
|
|
237
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
238
|
+
|
|
239
|
+
Returns: Object with detailed metadata for each attribute
|
|
240
|
+
|
|
241
|
+
Metadata Includes:
|
|
242
|
+
- type: Data type
|
|
243
|
+
- isRequired: Whether attribute exists
|
|
244
|
+
- path: Full path to attribute
|
|
245
|
+
- children: Nested attributes (for objects)
|
|
246
|
+
- length: Array length (for arrays)
|
|
247
|
+
- elementType: Type of array elements
|
|
248
|
+
|
|
249
|
+
### 16. getMetaCompact(jsonObject)
|
|
250
|
+
```javascript
|
|
251
|
+
const metaCompact = app.getMetaCompact(dataJSON);
|
|
252
|
+
// Output: Compact metadata format
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
**Description**: Creates a compact metadata representation showing the structure hierarchy.
|
|
256
|
+
|
|
257
|
+
**Parameters**:
|
|
258
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
259
|
+
|
|
260
|
+
Returns: Compact representation of JSON structure
|
|
261
|
+
|
|
262
|
+
Format Examples:
|
|
263
|
+
- "array[string]": Array of strings
|
|
264
|
+
- "array[object]": Array of objects
|
|
265
|
+
- Nested objects shown as nested objects
|
|
266
|
+
|
|
267
|
+
### 17. getPartial(jsonObject, attributes)
|
|
268
|
+
```javascript
|
|
269
|
+
const partial = app.getPartial(dataJSON, ["name", "age"]);
|
|
270
|
+
// Output: {"name": "John", "age": 30}
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
**Description**: Extracts specific attributes from a JSON object.
|
|
274
|
+
|
|
275
|
+
**Parameters**:
|
|
276
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
277
|
+
- attributes (Array|String|Object): Attributes to extract
|
|
278
|
+
|
|
279
|
+
attribute Parameter Types:
|
|
280
|
+
- Array: List of attribute names to extract
|
|
281
|
+
```javascript
|
|
282
|
+
app.getPartial(data, ["name", "age"])
|
|
283
|
+
```
|
|
284
|
+
- String: Single attribute name
|
|
285
|
+
```javascript
|
|
286
|
+
app.getPartial(data, "name")
|
|
287
|
+
```
|
|
288
|
+
- Object: Mapping of new names to original attributes
|
|
289
|
+
```javascript
|
|
290
|
+
app.getPartial(data, {"fullName": "name", "yearsOld": "age"})
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Returns: Object containing only the specified attributes
|
|
294
|
+
|
|
295
|
+
### 18. getPartialDeep(jsonObject, attributePaths)
|
|
296
|
+
```javascript
|
|
297
|
+
const deepPartial = app.getPartialDeep(dataJSON, ["name", "address.city", "hobbies.length"]);
|
|
298
|
+
// Output: {"name": "John", "city": "Jakarta", "length": 2}
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**Description**: Extracts attributes using nested paths (dot notation).
|
|
302
|
+
|
|
303
|
+
**Parameters**:
|
|
304
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
305
|
+
- attributePaths (Array|Object): Paths to extract
|
|
306
|
+
|
|
307
|
+
attributePaths Parameter Types:
|
|
308
|
+
- Array: List of dot-notated paths
|
|
309
|
+
```javascript
|
|
310
|
+
app.getPartialDeep(data, ["user.profile.name", "user.contact.email"])
|
|
311
|
+
```
|
|
312
|
+
- Object: Mapping of new names to paths
|
|
313
|
+
```javascript
|
|
314
|
+
app.getPartialDeep(data, {"userName": "user.profile.name", "userEmail": "user.contact.email"})
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
Returns: Object with extracted values (uses last path segment as key for array input)
|
|
318
|
+
|
|
319
|
+
### 19. getPartialWithDefaults(jsonObject, attributesConfig)
|
|
320
|
+
```javascript
|
|
321
|
+
const partialWithDefaults = app.getPartialWithDefaults(dataJSON, {
|
|
322
|
+
"name": "name",
|
|
323
|
+
"status": {
|
|
324
|
+
path: "isActive",
|
|
325
|
+
transform: (val) => val ? "Active" : "Inactive"
|
|
326
|
+
},
|
|
327
|
+
"email": {
|
|
328
|
+
path: "contact.email",
|
|
329
|
+
default: "no-email@example.com"
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
```
|
|
333
|
+
|
|
334
|
+
**Description**: Extracts attributes with advanced configuration including default values and transformations.
|
|
335
|
+
|
|
336
|
+
**Parameters**:
|
|
337
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
338
|
+
- attributesConfig (Object): Configuration object
|
|
339
|
+
|
|
340
|
+
Configuration Options:
|
|
341
|
+
- String: Simple path extraction
|
|
342
|
+
```javascript
|
|
343
|
+
"name": "user.fullName"
|
|
344
|
+
```
|
|
345
|
+
- Object: Advanced configuration
|
|
346
|
+
```javascript
|
|
347
|
+
"formattedAge": {
|
|
348
|
+
path: "age", // Required: Path to attribute
|
|
349
|
+
default: 0, // Optional: Default value if path doesn't exist
|
|
350
|
+
transform: (val) => ${val} years old // Optional: Transformation function
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
Returns: Object with extracted and processed values
|
|
355
|
+
|
|
356
|
+
### 20. excludeAttributes(jsonObject, attributesToExclude)
|
|
357
|
+
```javascript
|
|
358
|
+
const filtered = app.excludeAttributes(dataJSON, ["isActive", "address"]);
|
|
359
|
+
// Output: {"name": "John", "age": 30, "hobbies": ["reading", "coding"]}
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
**Description**: Creates a new JSON object excluding specified attributes.
|
|
363
|
+
|
|
364
|
+
**Parameters**:
|
|
365
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
366
|
+
- attributesToExclude (Array|String): Attributes to remove
|
|
367
|
+
|
|
368
|
+
attributesToExclude Parameter Types:
|
|
369
|
+
- Array: List of attribute names to exclude
|
|
370
|
+
```javascript
|
|
371
|
+
app.excludeAttributes(data, ["password", "secretKey"])
|
|
372
|
+
```
|
|
373
|
+
- String: Single attribute name to exclude
|
|
374
|
+
```javascript
|
|
375
|
+
app.excludeAttributes(data, "password")
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
Returns: New object without the excluded attributes
|
|
379
|
+
|
|
380
|
+
### 21. getAttributeValue(jsonObject, attributeName, defaultValue)
|
|
381
|
+
```javascript
|
|
382
|
+
const name = app.getAttributeValue(dataJSON, "name");
|
|
383
|
+
const email = app.getAttributeValue(dataJSON, "email", "default@email.com");
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
**Description**: Gets the value of a specific attribute with optional default value.
|
|
387
|
+
|
|
388
|
+
**Parameters**:
|
|
389
|
+
- jsonObject (Object|String): JSON object or JSON string
|
|
390
|
+
- attributeName (String): Name of the attribute to retrieve
|
|
391
|
+
- defaultValue (Any, optional): Default value if attribute doesn't exist
|
|
392
|
+
|
|
393
|
+
Returns: Attribute value or default value
|
|
394
|
+
|
|
395
|
+
Advanced Usage Examples
|
|
396
|
+
```javascript
|
|
397
|
+
const app = require('./index');
|
|
398
|
+
|
|
399
|
+
// Get all attributes from JSON
|
|
400
|
+
const data = await app.getData('./data.json');
|
|
401
|
+
const jsonData = app.readJSON(data);
|
|
402
|
+
const attributes = app.getAttributes(jsonData);
|
|
403
|
+
|
|
404
|
+
// Get metadata information
|
|
405
|
+
const meta = app.getMeta(jsonData);
|
|
406
|
+
const metaDetail = app.getMetaDetail(jsonData);
|
|
407
|
+
|
|
408
|
+
// Extract specific data
|
|
409
|
+
const userInfo = app.getPartial(jsonData, ["name", "email", "phone"]);
|
|
410
|
+
const nestedData = app.getPartialDeep(jsonData, ["user.profile.name", "user.contact.email"]);
|
|
411
|
+
|
|
412
|
+
// Extract with transformations and defaults
|
|
413
|
+
const processedData = app.getPartialWithDefaults(jsonData, {
|
|
414
|
+
"fullName": "user.name",
|
|
415
|
+
"ageFormatted": {
|
|
416
|
+
path: "user.age",
|
|
417
|
+
transform: (age) => ${age} years old
|
|
418
|
+
},
|
|
419
|
+
"country": {
|
|
420
|
+
path: "user.address.country",
|
|
421
|
+
default: "Unknown"
|
|
422
|
+
}
|
|
423
|
+
});
|
|
424
|
+
|
|
425
|
+
// Exclude sensitive information
|
|
426
|
+
const safeData = app.excludeAttributes(jsonData, ["password", "ssn", "creditCard"]);
|
|
427
|
+
|
|
428
|
+
// Analyze complex nested structures
|
|
429
|
+
const complexMeta = app.getMetaDetail(complexJSON);
|
|
430
|
+
console.log(complexMeta.user?.children?.contact?.children?.email?.type); // "string"
|
|
431
|
+
|
|
432
|
+
// Get single attribute value
|
|
433
|
+
const userName = app.getAttributeValue(jsonData, "name");
|
|
434
|
+
const userEmail = app.getAttributeValue(jsonData, "email", "no-email@example.com");
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
## Requirements
|
|
438
|
+
- Node.js 12 or higher
|
|
439
439
|
- Internet connection (for URL functions)
|