Check out our High Performance Batch Processing API: Match and Enrich Data Using CSV/TSV Files as Input Data to our APIs Learn More

JSON URL Encoding Code Examples

To use the batch processing capability of Interzoid's Full Dataset Matching API without using a source file, we must URL-encode JSON text to send it as a parameter to the API. Here are code examples to URL-encode the following JSON:


[
    { "Data": "IBM" },
    { "Data": "International Business Machines" },
    { "Data": "ibm corp" }
]


API: Full Dataset Matching

Register for API Key

Python

This example demonstrates how to URL-encode JSON using Python's built-in modules.


import json
import urllib.parse

def url_encode_json(data):
    # Convert the Python object to a JSON string
    json_string = json.dumps(data)

    # URL encode the JSON string
    encoded_string = urllib.parse.quote(json_string)

    return encoded_string

# Your JSON data
data = [
    { "Data": "IBM" },
    { "Data": "International Business Machines" },
    { "Data": "ibm corp" }
]

# Encode the data
encoded_result = url_encode_json(data)
print(encoded_result)
        

Node.js

This example shows how to URL-encode JSON using Node.js without external libraries.


const data = [
    { "Data": "IBM" },
    { "Data": "International Business Machines" },
    { "Data": "ibm corp" }
];

console.log(encodeURIComponent(JSON.stringify(data)));

Go

This example shows how to URL-encode JSON in Go.


package main

import (
    "encoding/json"
    "fmt"
    "net/url"
)

type DataItem struct {
    Data string `json:"Data"`
}

func main() {
    data := []DataItem{
        {Data: "IBM"},
        {Data: "International Business Machines"},
        {Data: "ibm corp"},
    }

    // Marshal to JSON
    jsonBytes, err := json.Marshal(data)
    if err != nil {
        fmt.Printf("Error marshaling JSON: %v\n", err)
        return
    }

    // URL encode the JSON string
    encoded := url.QueryEscape(string(jsonBytes))
    fmt.Println(encoded)
}

Java

This example shows how to URL-encode JSON in Java.


import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonEncoder {
    public static void main(String[] args) {
        try {
            // Create the data structure
            List<Map<String, String>> data = new ArrayList<>();

            Map<String, String> item1 = new HashMap<>();
            item1.put("Data", "IBM");

            Map<String, String> item2 = new HashMap<>();
            item2.put("Data", "International Business Machines");

            Map<String, String> item3 = new HashMap<>();
            item3.put("Data", "ibm corp");

            data.add(item1);
            data.add(item2);
            data.add(item3);

            // Convert to JSON string
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(data);

            // URL encode the JSON string
            String encoded = URLEncoder.encode(jsonString, StandardCharsets.UTF_8.toString());

            System.out.println(encoded);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

TypeScript

This example shows how to URL-encode JSON in TypeScript.


type DataItem = {
    Data: string;
};

const data: DataItem[] = [
    { Data: "IBM" },
    { Data: "International Business Machines" },
    { Data: "ibm corp" }
];

console.log(encodeURIComponent(JSON.stringify(data)));

Rust

This example shows how to URL-encode JSON in Rust.


use serde::Serialize;
use urlencoding::encode;

#[derive(Serialize)]
struct DataItem {
    Data: String,
}

fn main() {
    let data = vec![
        DataItem { Data: "IBM".to_string() },
        DataItem { Data: "International Business Machines".to_string() },
        DataItem { Data: "ibm corp".to_string() },
    ];

    let json_string = serde_json::to_string(&data).unwrap();
    println!("{}", encode(&json_string));
}

C#

This example shows how to URL-encode JSON in C#.


using System.Text.Json;
using System.Web;

var data = new[]
{
    new { Data = "IBM" },
    new { Data = "International Business Machines" },
    new { Data = "ibm corp" }
};

var jsonString = JsonSerializer.Serialize(data);
var encoded = HttpUtility.UrlEncode(jsonString);
Console.WriteLine(encoded);

R

This example shows how to URL-encode JSON in R.


# Install jsonlite if you haven't already
# install.packages("jsonlite")
library(jsonlite)

# Create the data structure
data <- list(
  list(Data = "IBM"),
  list(Data = "International Business Machines"),
  list(Data = "ibm corp")
)

# Convert to JSON and URL encode
json_string <- toJSON(data, auto_unbox = TRUE)
encoded <- utils::URLencode(json_string, reserved = TRUE)
print(encoded)

Scala

This example shows how to URL-encode JSON in Scala.


import spray.json._
import DefaultJsonProtocol._
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

// Define the case class for our data structure
case class DataItem(Data: String)

// Define the JSON protocol
object JsonProtocol extends DefaultJsonProtocol {
  implicit val dataItemFormat: RootJsonFormat[DataItem] = jsonFormat1(DataItem)
}

import JsonProtocol._

object JsonEncoder extends App {
  // Create the data
  val data = List(
    DataItem("IBM"),
    DataItem("International Business Machines"),
    DataItem("ibm corp")
  )

  // Convert to JSON and URL encode
  val jsonString = data.toJson.compactPrint
  val encoded = URLEncoder.encode(jsonString, StandardCharsets.UTF_8.toString)

  println(encoded)
}