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

Retrieve all values that match a json path #743

Closed
ghost opened this issue Sep 14, 2017 · 5 comments
Closed

Retrieve all values that match a json path #743

ghost opened this issue Sep 14, 2017 · 5 comments
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation

Comments

@ghost
Copy link

ghost commented Sep 14, 2017

Assuming that my json file is the following, I would like to collect all values that match with a regular expression using json_pointer. How can I get all values (integers) in the path /array/a/*?

I have used flatten() so far, that gives the "keys". But I don't know if I can query the json object to return an array of values that "match" a json path.

{
 "array": [
    {
      "a": 1,
      "b": [10,11,12]
    },
    {
      "a": 2,
      "b": [20,21,22]
    },
    {
      "a": 3,
      "b": [30,31,32]
    }
  ],
  "cost": 1
}
@nlohmann
Copy link
Owner

What about

#include "json.hpp"
#include <iostream>
#include <regex>

using json = nlohmann::json;

int main(int argc, char const *argv[])
{
  json j = R"({
    "array": [
    {
      "a": 1,
      "b": [10,11,12]
    },
    {
      "a": 2,
      "b": [20,21,22]
    },
    {
      "a": 3,
      "b": [30,31,32]
    }
    ],
    "cost": 1
  })"_json;

  auto f = j.flatten();

  std::smatch sm;
  std::regex re("/array/[^/]+/a");

  json result;

  for (auto it = f.begin(); it != f.end(); ++it)
  {
    std::regex_search(it.key(), sm, re);
    if(not sm.empty())
    {
      result.push_back(it.value());
    }
  }

  std::cout << result << std::endl;        
}

@ghost
Copy link
Author

ghost commented Sep 14, 2017

Hello,

thank you for the follow up. I have already used this approach as a workaround. I was hoping that the library could take /array/*/a and reply back with a "vector" of values.

Thanks.

@nlohmann
Copy link
Owner

No, but you must admit that this is a very specific usecase... And I think you could clean up the code above and use some <algorithm> magic.

@nlohmann nlohmann added kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation labels Sep 14, 2017
@ghost
Copy link
Author

ghost commented Sep 14, 2017

True, it is a specific issue.

What kind of magic do you have in mind :-) ?

@nlohmann
Copy link
Owner

I don't know, maybe something like std::copy_if.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question solution: proposed fix a fix for the issue has been proposed and waits for confirmation
Projects
None yet
Development

No branches or pull requests

1 participant