Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a JSON file that is used in a monitoring software for monitor an specific device.
I want to monitor a similar device which I don't have the JSON file.
I know everything from the new device that is needed to "fill the blanks" in the existent JSON structure.
A brute force approach would be create a script that reads the data from new device from a craft input file and output the nodes and leafs in the same JSON structure.
Before I take this path I would like to know if there is some tool that could help me in this task. For sure this "wheel" is not new, I don't want to re-invent it again.
Anyone knows about a tool that uses a JSON as template and generate another one changing the values from other source ?
I strongly suggest that you should handle all JSON and XML data in the same way: with "magic encoder/decoder rings." First, decode the JSON into a data structure, then change that data structure as required. Finally, encode the data structure into a new JSON file. You never attempt to manipulate the data "as text." The encoder/decoder logic is industry-standard, common among every programming language these days that supports the file format, and known to be correct in all cases. You treat it as a "black box."
I am not familiar with "magic encoder/decoder rings." What do you mean ? Can you elaborate, please ?
How it is your suggestion different than the approach I suggested ? I mean, reading a data in a certain structure (plain file) and output in another structure (json) ?
(There is nothing "brute force" about this approach.)
Yeah, my first "brute" force approach was in python, using json library and json.load to initialize a dictionary. This creates the output structure.
Then I iterate over the keys, replacing the original value from json template read before with new values from a plain file.
This mapping from new values to the equivalent key in json is hardcoded on the code, what I dislike.
So I thought someone could give me a better idea how to map the raw source data into the json output, not hardcoded in the script itself.....
I mean,
data = json.load (json_file)
....
data['key1']= 'comes from 2nd line of data file'
data['key2']= 'comes from 10th line of data file'
data['key3']= 'comes from 3rd line of data file'
...
this is why I called brute force.....this mapping style is not good, even worse if hardcoded.
Virtually every programming language has a built-in capability to "encode and decode JSON," and in general AFAIK they use the samebinary libraries to do the heavy lifting. This means that there is an excellent chance that they will all be "compatible with one another," so long as you treat each language's built-in facilities as "a black box." As a "magic encoder/decoder ring" that can be relied upon to work in all cases. You do not peer into how it works its magick ... you only implicitly trust that it does.
Last edited by sundialsvcs; 10-26-2022 at 07:55 PM.
Well, you could put the x-ref mapping into it's own file eg transform.dat and have the program read that when it wants to translate from one file to the other.
In that case people only have to be able to edit a simple key=val list file for changes.
The program would then just read that (probably into hash), then go through the i/p file and pull out the relevant key for each entry to 'translate'.
Of course your program should use the std JSON libs to read/write the json files.
So I thought someone could give me a better idea how to map the raw source data into the json output, not hardcoded in the script itself.....
That was merely a simple example to indicate the point.
If you want a better idea how to map the "raw source data" then provide clearer information as to what this source data actually is - from your references to "plain file" and "(2nd|3rd|10th) line of data file" it sounds like your source might be newline-delimited data?
In which case you need some form of mapping to associates line numbers to variables, or variables to line numbers.
Whether done via code or data, anything which explicitly maps variable a to value b is not brute force, because brute force is when you try every possible combination - i.e. by looping through all the variables/values and testing each one.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.