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

JSON Data Mapping Key-Value from other Key-Value #2148

Closed
sakibnaz opened this issue May 28, 2020 · 10 comments
Closed

JSON Data Mapping Key-Value from other Key-Value #2148

sakibnaz opened this issue May 28, 2020 · 10 comments

Comments

@sakibnaz
Copy link

Hi. It may very basic question ...

I want to map a Key-Value from another key-Value. Like, for example:

{
Key1: "Test",
Key2: "This will be Value of Key1"
}

In want the Key2 will be Value of Key1 i.e. "Test" when parsing.

Is there any way to define the JSON in such way?

Thanks.

@FrancoisChabot
Copy link
Contributor

FrancoisChabot commented May 28, 2020

If you want to be able to create references like this, you may want to look at yaml (which is a superset of json) instead.

It would look like this:

{
   "key1" : &k1 "Test",
   "key2" : *k1
}

Again: This will not work with this library, or any other json library for that matter. This syntax is specific to yaml.

@nlohmann
Copy link
Owner

@FrancoisChabot is right. Or do you just want to copy a value?

@sakibnaz
Copy link
Author

Thanks for all reply.

@nlohmann , yes actually I want to copy values. Basically in my datasets I'll have some large array's where multiple array will have same data values. Hence rather writing it manually if I can copy values from one key to another within the json, it will be great. Is it possible here?

Regards.

@nlohmann
Copy link
Owner

The following is possible: j["Key2"] = j["Key1"];.

@sakibnaz
Copy link
Author

Understand ... but its inside the c++ code. I wanted to do it in the .json file itself.

@nlohmann
Copy link
Owner

Then I would need a concrete code example.

@nlohmann
Copy link
Owner

Meaning, I don't know exactly what you try to achieve.

@sakibnaz
Copy link
Author

sakibnaz commented May 29, 2020

Ok, let me explain one again may be it will help to clarify.

I have a json file hello.json like below:

{
Key1: "Test",
Key2: ???
}

Here my need is ... if I parse the Value of Key2 it will be Value of Key1 i.e. "Test". So is it possible to define the Value of Key2 in such a way in .json file, so it will contain Value of Key1 internally?

Thanks.

@nlohmann
Copy link
Owner

Then @FrancoisChabot understood you right, and this is not possible with JSON and this library.

@sakibnaz
Copy link
Author

Noted with Thanks,

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

No branches or pull requests

3 participants