LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 10-26-2022, 09:17 AM   #1
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,501
Blog Entries: 2

Rep: Reputation: 68
how to create a JSON file based on a template


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 ?

any ideas ?
 
Old 10-26-2022, 11:15 AM   #2
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,706
Blog Entries: 4

Rep: Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949
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."
 
Old 10-26-2022, 01:43 PM   #3
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,501

Original Poster
Blog Entries: 2

Rep: Reputation: 68
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) ?
 
Old 10-26-2022, 02:28 PM   #4
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,644

Rep: Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563

I think the English translation of what sundialsvcs is attempting to say is: Use the standard JSON library for the language of your choice.

For example, in pseudocode:
Code:
config = json.deserialize(file.read('/path/to/template.json'))
config.variable1 = 'something'
config.variable2 = [1,2,3]
config.variable4 = True
file.write('/path/to/output.json',json.serialize(config))
(There is nothing "brute force" about this approach.)

If you don't already have a chosen language, you can probably use jq, though I don't find its documentation very good.

 
Old 10-26-2022, 03:54 PM   #5
marozsas
Senior Member
 
Registered: Dec 2005
Location: Campinas/SP - Brazil
Distribution: SuSE, RHEL, Fedora, Ubuntu
Posts: 1,501

Original Poster
Blog Entries: 2

Rep: Reputation: 68
Quote:
Originally Posted by boughtonp View Post
(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.

Last edited by marozsas; 10-26-2022 at 03:56 PM.
 
Old 10-26-2022, 07:53 PM   #6
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,706
Blog Entries: 4

Rep: Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949Reputation: 3949
"Yes, @boughtonp, that's exactly what I meant."

Virtually every programming language has a built-in capability to "encode and decode JSON," and in general AFAIK they use the same binary 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.
 
Old 10-26-2022, 11:04 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
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.

Last edited by chrism01; 10-26-2022 at 11:05 PM.
 
Old 10-27-2022, 02:44 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,109

Rep: Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368Reputation: 7368
you need to create a map file, which will tell you the details:
Code:
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 can be translated to something like that
 
Old 10-27-2022, 08:06 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,644

Rep: Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563Reputation: 2563
Quote:
Originally Posted by marozsas View Post
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.

 
  


Reply

Tags
json, template



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Retrieve the content of a json file using shell script .Without jq /json Historia Linux - Newbie 12 03-25-2020 09:50 AM
LXer: JSON Lines: record-style JSON LXer Syndicated Linux News 0 01-28-2020 08:03 PM
Resume creator - export options : template vs non-template approach vharishankar Programming 1 12-07-2011 11:11 PM
from django.template import Template, gives output firedancer Linux - Newbie 0 11-30-2007 02:08 PM
Template class with a template member... Nicholas Bishop Programming 3 02-21-2005 08:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 11:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration