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

items() unable to get the elements #1375

Closed
dineshkumar02 opened this issue Nov 25, 2018 · 6 comments
Closed

items() unable to get the elements #1375

dineshkumar02 opened this issue Nov 25, 2018 · 6 comments

Comments

@dineshkumar02
Copy link

  • What is the issue you have?
    Unable to get each element from the json object.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?
    I am getting the following response from one for the server, and I am trying to de-serialize the string to json, and trying to fetch each item from the "message" object.

Output from server
{"error":"","message":"{\"auto_disable_count\":0,\"complete_flow_result_match\":\"\",\"enable\":true,\"fail_event_ids\":null,\"fail_result_match\":\"\",\"frequency\":\"Day: * Month: * Hour:* Minute:* Second: *\",\"generate_run\":false,\"id\":1,\"name\":\"test os job\",\"pass_event_ids\":null,\"run\":{\"kind\":\"os\",\"properties\":{\"command\":\"testProcess\",\"environment\":null,\"fail_exit_code\":0},\"type\":\"job\"}}"}

But, the following code is not iterating over the "message" object, rather it's giving the whole object as a single string.

	json                table_data;
	table_data[ "data" ] = json::parse( data[ "message" ].dump() )

	for ( auto& x : table_data["data"].items() ) {
		std::cout << "key is " << x.key() << std::endl;
		std::cout << "value is " << x.value() << std::endl;
        }
key is
value is "{\"auto_disable_count\":0,\"complete_flow_result_match\":\"\",\"enable\":true,\"fail_event_ids\":null,\"fail_result_match\":\"\",\"frequency\":\"Day: * Month: * Hour:* Minute:* Second: *\",\"generate_run\":false,\"id\":1,\"name\":\"test os job\",\"pass_event_ids\":null,\"run\":{\"kind\":\"os\",\"properties\":{\"command\":\"testProcess\",\"environment\":null,\"fail_exit_code\":0},\"type\":\"job\"}}"
  • What is the expected behavior?
    Isn't the items() method suppose to give all items in "message" object ?.

  • And what is the actual behavior instead?
    items() method is not returning the each element value.

  • Which compiler and operating system are you using? Is it a supported compiler?
    Visual Studio 2017 Developer Command Prompt v15.8.5

Kindly let me know if I am missing anything here.

@dineshkumar02 dineshkumar02 changed the title items() items() unable to get the elements Nov 25, 2018
@vineethkartha
Copy link

@dineshkumar02 I tried copying the error object JSOn code into https://jsoneditoronline.org/
This is a tool that can show the structure of the JSON object and in this tool it shows that the json object has just two keys

  1. error - with empty value
  2. message - with the entire string as its value.

Could you please re-check and confirm this.

@dineshkumar02
Copy link
Author

@vineethkartha thanks for the validation.

Yes, there are two objects. "error" object is empty and "message" object has the actual content, which I am trying to get the elements using items().

@gregmarr
Copy link
Contributor

I think your problem is the .dump() here, try removing it:

table_data[ "data" ] = json::parse( data[ "message" ].dump() )

@dineshkumar02
Copy link
Author

@gregmarr If I remove the .dump() then json::parse() is giving compilation error as no overloaded function with the given argument.

I tried with below code and the problem still same.

json table_data; table_data[ "data" ] = data[ "message" ];

key is
value is "{\"auto_disable_count\":0,\"complete_flow_result_match\":\"\",\"enable\":true,\"fail_event_ids\":null,\"fail_result_match\":\"\",\"frequency\":\"Day: * Month: * Hour:* Minute:* Second: *\",\"generate_run\":false,\"id\":1,\"name\":\"test os job\",\"pass_event_ids\":null,\"run\":{\"kind\":\"os\",\"properties\":{\"command\":\"testProcess\",\"environment\":null,\"fail_exit_code\":0},\"type\":\"job\"}}"

@chris0x44
Copy link

Calling data[ "message" ] will give you a JSON object containing a string. You want to parse this string into a new JSON object.

The hint by @gregmarr is correct in that you should not dump the contents because this will just create a string-representation of the object you already have. Instead you need to get the contained string from the object. For example like this:

table_data[ "data" ] = json::parse( data[ "message" ].get<std::string>() );

@dineshkumar02
Copy link
Author

Thanks @chris0x44 and @gregmarr .

After removing the .dump() and by using .getstd::string() things are working as expected, and able to get the items from the object.

I am closing this case, as I got the desired results.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants