Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I traverse a json object and add new elements into the hierarchy #1834

Closed
michael-a-green opened this issue Nov 9, 2019 · 5 comments
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated

Comments

@michael-a-green
Copy link

michael-a-green commented Nov 9, 2019

** - Describe what you want to achieve. **

I want to generate a JSON object that has hierarchy. Something like this:

{
    "mything": "simple_thing",
    "thing_projects": {
        "name": "Project A",
        "revisions": [
            {
                "revision":1,
                "name":"particular_thing"
                "children":[{"name":"part1_of_particular_thing"},{"name":"part2_of_particular_thing"}]
            },
            {
                "revision" : 22
            }
        ]
    }
}

I want to create this on the fly as I'm traversing another data structure. But I don't know how to traverse to arbitrary locations in a json object.

** - Describe what you tried. **

I've tried this:

json::iterator it;
  for (it = (*userdata_jptr).begin(); it != (*userdata_jptr).end(); ++it){

    if (it.key() == "name" && it.value() == "Project A") {
      break;
    }
  }

  it["name"] = temp_scope_name;
  it["scope_type"] = temp_scopetype_name;

where userdata_jptr is a pointer to the json object I'm trying to construct.

But when I try to compile this code I get errors like this:

src/main.cpp: In function ‘void myScope(ucisT, ucisScopeT, json*, int)’:
/src/main.cpp:375:6: error: invalid conversion from ‘const char*’ to ‘nlohmann::detail::iter_impl<nlohmann::basic_json<> >::difference_type’ {aka ‘long int’} [-fpermissive]
  375 |   it["name"] = temp_scope_name;
      |      ^~~~~~
      |      |
      |      const char*
In file included from src/main.cpp:27:

What is the right iterator type to use when I want something that will point to the location in the json object to which I want to make changes (just adding things at this point).

Or in general what is the right method to traverse to a certain location in a json object, and then use whatever mechanism was used to iterate to that location and make in-place changes?

** - Describe which system (OS, compiler) you are using. **

Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib64/gcc/x86_64-suse-linux/9/lto-wrapper
OFFLOAD_TARGET_NAMES=hsa:nvptx-none
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64 --enable-languages=c,c++,objc,fortran,obj-c++,ada,go,d --enable-offload-targets=hsa,nvptx-none=/usr/nvptx-none, --without-cuda-driver --disable-werror --with-gxx-include-dir=/usr/include/c++/9 --enable-ssp --disable-libssp --disable-libvtv --disable-cet --disable-libcc1 --enable-plugin --with-bugurl=https://bugs.opensuse.org/ --with-pkgversion='SUSE Linux' --with-slibdir=/lib64 --with-system-zlib --enable-libstdcxx-allocator=new --disable-libstdcxx-pch --enable-libphobos --enable-version-specific-runtime-libs --with-gcc-major-version-only --enable-linker-build-id --enable-linux-futex --enable-gnu-indirect-function --program-suffix=-9 --without-system-libunwind --enable-multilib --with-arch-32=x86-64 --with-tune=generic --with-build-config=bootstrap-lto-lean --enable-link-mutex --build=x86_64-suse-linux --host=x86_64-suse-linux
Thread model: posix
gcc version 9.2.1 20190820 [gcc-9-branch revision 274748] (SUSE Linux) 
LSB Version:	n/a
Distributor ID:	openSUSE
Description:	openSUSE Tumbleweed
Release:	20190921
Codename:	n/a

Linux <machine name> 5.2.14-1-default #1 SMP Tue Sep 10 10:52:01 UTC 2019 (374b0ae) x86_64 x86_64 x86_64 GNU/Linux

  • Describe which version of the library you are using (release version, develop branch).

Version 3.7.0.1-1

Thanks

@nlohmann
Copy link
Owner

nlohmann commented Nov 9, 2019

The error message looks like as if you would like to access an array (only integer indices are allowed) with a key. The latter is only allowed for objects.

@michael-a-green
Copy link
Author

The error message looks like as if you would like to access an array (only integer indices are allowed) with a key. The latter is only allowed for objects.

Thanks for the response. How should I do it?

@psotos
Copy link

psotos commented Nov 16, 2019

I would also like to know the answer to this. Thanks!

@nlohmann
Copy link
Owner

This is hard to answer in general. Can you provide a small example?

@stale
Copy link

stale bot commented Dec 18, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated label Dec 18, 2019
@stale stale bot closed this as completed Dec 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question state: stale the issue has not been updated in a while and will be closed automatically soon unless it is updated
Projects
None yet
Development

No branches or pull requests

3 participants