Skip to content

Creating an empty array #27

Description

@edi9999

I'm using the following code right now:

json customers;

for (...)
{
  customers.push_back(...)
}

cout << customers;

However, if the for loop doesnt happen, I would like to return an empty array (eg [])

Is they an other way to get this behaviour without doing json customers= json::parse("[]"); ?

Activity

  1. Teemperor commented on Jan 13, 2015

    @Teemperor
    Contributor

    I assume this is fixed by fixing #8

  2. nlohmann commented on Jan 13, 2015

    @nlohmann
    Owner

    @Teemperor is right - this shall be fixed with issue #8. I hope to do this by tomorrow.

    So far, you could try

    json customers(json::value_type::array);

    to tell the class to create an empty JSON array.

  3. added a commit that references this issue on Jan 24, 2015
  4. self-assigned this
    on Jan 24, 2015
  5. nlohmann commented on Jan 24, 2015

    @nlohmann
    Owner

    There now is a function json::array() to create an empty array. It can also be used with an initializer list (e.g., json::array({"foo", "bar"})) to explicitly create the array ["foo", "bar"] rather than the object {"foo": "bar"}.

  6. added a commit that references this issue on May 7, 2021
  7. added a commit that references this issue on Dec 16, 2025
  8. added a commit that references this issue on Jan 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions