Json

Working with JSON Functions Example

  1. Importing Functions:

    const { newCommand, jsonParse, jsonPretty, json, jsonExists, jsonSet, jsonStringify, jsonUnset, jsonClear } = require('mvk-project');

    First, we import the necessary JSON-related functions from the mvk-project package:

    • newCommand: Used to define a new command.

    • jsonParse: Parses a JSON string into a JavaScript object.

    • jsonPretty: Formats the JSON object into a pretty-printed string.

    • json: Retrieves the value of a specific key from the JSON object.

    • jsonExists: Checks if a specific key exists in the JSON object.

    • jsonSet: Sets a new key-value pair in the JSON object.

    • jsonStringify: Converts the JSON object back into a string.

    • jsonUnset: Removes a key-value pair from the JSON object.

    • jsonClear: Clears all key-value pairs in the JSON object.

  2. Defining a New Command:

    newCommand({
      name: 'test-command',
      code: async function (message) {
      }
    });

    We define a new command named test-command. This command will be executed when invoked, and it will manipulate a JSON object in various ways.

  3. Parsing and Manipulating JSON:

    const iJson = '{ "name":"Alex", "years":"18", "adult":"true" }';
    jsonParse(iJson);

    We start by defining a JSON string iJson. The jsonParse function is then used to parse this string into a JavaScript object, allowing us to manipulate it.

  4. Using JSON Functions:

    await message.reply(`jsonPretty: ${jsonPretty(4)}, json (name): ${json({ key: 'name' })}\njsonExists (years and address): years: ${jsonExists({ key: 'years'})}, address: ${jsonExists({ key: 'address'})},\njsonSet and jsonStringify: ${jsonSet({ key: 'address', value: 'Street 1085' })} ${jsonStringify()},\njsonUnset: ${jsonUnset({ key: 'adult' })} ${jsonStringify()},\njsonClear: ${jsonClear()} ${jsonStringify()} `);
    • jsonPretty(4): Formats the JSON object into a pretty-printed string with an indentation of 4 spaces.

    • json({ key: 'name' }): Retrieves the value associated with the key name.

    • jsonExists({ key: 'years' }) and jsonExists({ key: 'address' }): Checks if the keys years and address exist in the JSON object, returning true or false.

    • jsonSet({ key: 'address', value: 'Street 1085' }): Sets a new key-value pair address: 'Street 1085' in the JSON object.

    • jsonStringify(): Converts the modified JSON object back into a string format.

    • jsonUnset({ key: 'adult' }): Removes the key adult from the JSON object.

    • jsonClear(): Clears all key-value pairs from the JSON object.

  5. Final Code:

    const { newCommand, jsonParse, jsonPretty, json, jsonExists, jsonSet, jsonStringify, jsonUnset, jsonClear } = require('../index');
    
    newCommand({
      name: 'test-command',
      code: async function (message) {
        const iJson = '{ "name":"Alex", "years":"18", "adult":"true" }';
        jsonParse(iJson);
        await message.reply(`jsonPretty: ${jsonPretty(4)}, json (name): ${json({key: 'name' },)}\njsonExists (years and address): years: ${jsonExists({ key: 'years'})}, address: ${jsonExists({key: 'address'})},\njsonSet and jsonStringify: ${jsonSet({key: 'address', value: 'Street 1085'})} ${jsonStringify()},\njsonUnset: ${jsonUnset({ key: 'adult' })} ${jsonStringify()},\njsonClear: ${jsonClear()} ${jsonStringify()} `)
      }
    });

Function Descriptions:

This example demonstrates how to parse a JSON string, manipulate its content using various functions, and then convert it back into a string. Each function allows for different types of interactions with the JSON object, making it a powerful tool for handling JSON data in your bot commands.

Last updated