DeveloperBreeze

JSON Serialization and Deserialization

import json

# Dictionary representing data
data = {'name': 'John', 'age': 30, 'city': 'New York'}

# Serialize (convert to JSON)
json_string = json.dumps(data)

# To parse JSON data (deserialize)
parsed_data = json.loads(json_string)

Related Posts

More content you might like

Tutorial
javascript

التعامل مع JSON في JavaScript: قراءة البيانات وكتابتها

في JavaScript (في بيئات Node.js)، يمكنك أيضًا كتابة بيانات JSON إلى ملف. أولاً، تحتاج إلى تحويل الكائن إلى JSON باستخدام JSON.stringify()، ثم استخدام الوحدة fs لكتابة البيانات إلى ملف.

const fs = require('fs');

const person = {
    name: "أحمد",
    age: 30,
    isMarried: false,
    children: ["سارة", "علي"]
};

const jsonString = JSON.stringify(person, null, 2);

fs.writeFile('person.json', jsonString, (err) => {
    if (err) {
        console.error('حدث خطأ أثناء الكتابة إلى الملف', err);
    } else {
        console.log('تم كتابة الملف بنجاح!');
    }
});

Sep 26, 2024
Read More
Tutorial
javascript

AJAX with JavaScript: A Practical Guide

In many real-world applications, you'll need to send data to the server using POST requests. Here's how to send form data using the Fetch API.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>POST Request with Fetch</title>
</head>
<body>
    <form id="postDataForm">
        <input type="text" id="title" placeholder="Enter title" required>
        <input type="text" id="body" placeholder="Enter body" required>
        <button type="submit">Submit</button>
    </form>
    <div id="postDataOutput"></div>

    <script>
        document.getElementById('postDataForm').addEventListener('submit', function(e) {
            e.preventDefault();

            const title = document.getElementById('title').value;
            const body = document.getElementById('body').value;

            fetch('https://jsonplaceholder.typicode.com/posts', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    title: title,
                    body: body
                })
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('postDataOutput').innerHTML = `
                    <h3>Data Submitted:</h3>
                    <p>Title: ${data.title}</p>
                    <p>Body: ${data.body}</p>
                `;
            })
            .catch(error => console.error('Error:', error));
        });
    </script>
</body>
</html>

Sep 18, 2024
Read More
Tutorial
go

Building a RESTful API with Go and Gorilla Mux

You can use a tool like Postman or curl to test the endpoints.

   curl -X GET http://localhost:8000/books

Aug 12, 2024
Read More
Code
javascript json

How to Deep Clone a JavaScript Object

// Using JSON methods
const deepClone = (obj) => {
    return JSON.parse(JSON.stringify(obj));
};

// Using structured cloning (modern browsers)
const deepCloneModern = structuredClone(obj);

// Usage
const original = { a: 1, b: { c: 2 } };
const clone = deepClone(original);

Aug 12, 2024
Read More

Discussion 0

Please sign in to join the discussion.

No comments yet. Be the first to share your thoughts!