5

My primary need is how to find the number of elements in top level data.

Given the json data:

{
 [
  {Key11:Value11,Key12:Value12,...Key1N:Value1N},
  {Key21:Value21,Key22:Value22,...Key2N:Value2n},
  {Key31:Value31,Key32:Value32,...Key3N:Value3N},
  {Key41:Value41,Key42:Value42,...Key4N:Value4N},
  .
  .
  .
  KeyZ1:ValueZ1,KeyZ2:ValueZ2,...KeyZN:ValueZN}
 ]
}

how do I find the number of array elements in the Json data?

Also, given the jason data:

{
 {Key11:Value11,Key12:Value12,...Key1N:Value1N}
}

How do I find the number of key-value elements in the json data?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
user2779124
  • 79
  • 1
  • 7

1 Answers1

8

You can use the size member function of a Json::Value object, like so. Your data wasn't workable, so I got some from somewhere else but I think you'll see the commonalities.

The json data you posted is not syntactically correct. To make it valid you must either name the list element or remove the outer {}. I have provided solutions for both possibilities

#include <cstdio>
#include <cstring>
#include <iostream>

#include "json/json.h"

using namespace std;

void named()
{
    string txt = "{                 \
    \"employees\": [{               \
        \"firstName\": \"John\",    \
        \"lastName\": \"Doe\"       \
    }, {                            \
        \"firstName\": \"Anna\",    \
        \"lastName\": \"Smith\"     \
    }, {                            \
        \"firstName\": \"Peter\",   \
        \"lastName\": \"Jones\"     \
    }]                              \
    }";


    Json::Value root;
    Json::Reader reader;

    bool ok = reader.parse(txt, root, false);

    if(! ok) 
    {
        cout << "failed parse\n";
        return;
    }

    cout << "parsed ok\n";

    // Answer to question 1
    cout << "The employee list is size " << root["employees"].size() << '\n';

    for(const auto& jv: root["employees"])
    {
        // Answer to your second question
        cout << "employee " << jv["firstName"] << " has " << jv.size() << " elements\n";
    }
}

void unnamed()
{
    string txt2 = "[{               \
        \"firstName\": \"John\",    \
        \"lastName\": \"Doe\"       \
    }, {                            \
        \"firstName\": \"Anna\",    \
        \"lastName\": \"Smith\"     \
    }, {                            \
        \"firstName\": \"Peter\",   \
        \"lastName\": \"Jones\"     \
    }]";

    Json::Value root;
    Json::Reader reader;

    bool ok = reader.parse(txt2, root, false);

    if(! ok) 
    {
        cout << "failed parse\n";
        return;
    }

    cout << "parsed ok\n";

    // Answer to question 1
    cout << "The employee list is size " << root.size() << '\n';

    for(const auto& jv: root)
    {
        // Answer to your second question
        cout << "employee " << jv["firstName"] << " has " << jv.size() << " elements\n";
    }
}

int main()
{
    named();
    unnamed();
}
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • So, suppose your samlejson data does not have the employee label.Suppose it simply starts by listing the first na – user2779124 Aug 10 '16 at 07:25
  • is that valid json? – Paul Rooney Aug 10 '16 at 07:25
  • Its valid if you remove the outer set of `{}`. – Paul Rooney Aug 10 '16 at 07:34
  • So, suppose your samlejson data does not have the employee label.Suppose it simply starts by listing the first names and surnames.Or is that not allowed.How to tacke suchis the aim of .y quest. I actually already know the soution to the example you ga e. If you remove the label employee label and just list the key:value as array. How would the solution be. – user2779124 Aug 10 '16 at 07:42
  • Did you try my updated answer? You simply need to call size on whichever node you want the size of. If you go with the array only option then the array is the root node itself. – Paul Rooney Aug 10 '16 at 07:45
  • Ok.If one removes the outer {} and the employee label, how will the solution be. – user2779124 Aug 10 '16 at 07:46
  • Istead of root["employee].size(), what will it be now? – user2779124 Aug 10 '16 at 07:52
  • Please,just one more thing.Would it make sense for me to list the elements of a list with numerical idices like root[1] ? – user2779124 Aug 10 '16 at 11:44
  • Did you try it? Check the docs for an `operator[]` overload. I suspect it probably does for an array/list but not a dictionary like item. – Paul Rooney Aug 10 '16 at 13:17
  • OK.I'll do that.Thanks. – user2779124 Aug 10 '16 at 13:38