Match one JSON scheme with another JSON scheme

I have a bunch of JSON files, thousands of different schemas. Using GenSON (the Python JSON schema generator) , I was able to create schema files for each of the input files. Now, what I would like to do is standardize all of these different files for one specific scheme. Here is an example:

Enter

 { "name": "Bob Odenkirk", "title": "Software Engineer", "location": { "locality": "San Francisco", "region": "CA", "country": "United States" }, "age": 62, "status": "Active" } 

Output

 { "names": ["Bob Odenkirk"], "occupations": ["Software Engineer"], "locations": ["San Francisco, CA"] } 

Essentially, I'm looking for an agnostic method of the language (i.e. I don't care which programming language is used) to determine how the input JSON file to be processed into the output JSON file.

+5
source share
4 answers

The URL https://github.com/bazaarvoice/jolt#jolt says that Jolt may be what you are looking for.

Push

The JSON conversion library for JSON, written in Java, where the "specification" for the conversion itself is a JSON document.

Useful for

Convert JSON data from ElasticSearch, MongoDb, Cassandra, etc. before shipping to the world

Extract data from large JSON documents for your own consumption

+3
source

I'm not sure what you expect, as shown below. A long time ago, I created an object with a flat object and an output format. It will return an output format object with filled data.

 var input = { "name": "Bob Odenkirk", "title": "Software Engineer", "location": { "locality": "San Francisco", "region": "CA", "country": "United States" }, "age": 62, "status": "Active" }; var outputFormat = { "name": "name", "occupations": "title", "locations": "location.locality, location.region" }; var flatInput = {}; function generateFlatInput(input, parent){ for (var prop in input) { if(input.hasOwnProperty(prop) && typeof input[prop] === 'object') flatInput = generateFlatInput(input[prop], parent + prop + '.'); else flatInput[parent + prop] = input[prop]; } return flatInput; } function generateOutput(input, outputFormat, delimiter){ input = generateFlatInput(input, ''); for (var prop in outputFormat) { var fields = outputFormat[prop].split(delimiter); var fieldValue = []; for(i = 0; i < fields.length; i++){ if(!input.hasOwnProperty(fields[i].trim())) continue; fieldValue.push(input[fields[i].trim()]); } outputFormat[prop] = fieldValue.join(delimiter); } return outputFormat; } console.log(generateOutput(input, outputFormat, ', ')); 

https://jsfiddle.net/u2yyuguk/1/

+1
source

Jolt spec

 [ // First build the "city, state" string for location { "operation": "modify-default-beta", "spec": { "location": { "locConcat": "=concat(@(1,locality),', ',@(1,region))" } } }, // Then map the fields as needed to positions in an output json { "operation": "shift", "spec": { "name": "name[0]", "title": "occupations[0]", "location": { "locConcat": "locations[0]" } } } ] 
+1
source

I think the best, fastest and easiest way to parse many JSON files is to use python.
I did something similar to your project and ran into the same problem. I found this site that teaches how to use python to actually parse JSON files. It turns out there is a python library called json (use pip to load json dependencies) that allows you to process JSON files. If you already have a python editor, this method will be simpler and faster than using Jolt Check this site for more information: https://code.tutsplus.com/tutorials/how-to-work-with-json-data -using-python - cms-25758 .
You can also use JS, which is again faster than Jolt. this is the website: https://docs.microsoft.com/en-us/scripting/javascript/reference/json-parse-function-javascript . It is very simple since you can use the JSON.parse() function

0
source

Source: https://habr.com/ru/post/1273650/


All Articles