How to iterate on json fields and insert new values using json4s? - json

I have a simple json file:
val myJson = {
"field1": [
{
"name": "john",
"lname": "knight"
},
{
"name": "jack",
"lname": "samuel"
},
{
"name": "elinor",
"lname": "cooper"
}
],
"field2": [
{
...
},
{
...
},
{
...
}
],
"field3": [
{
...
},
{
...
},
{
...
}
]
}
and what i want is to be able to iterate on "field1" and for each name to call a method that returns some value and insert this value to the json under "fiel1".
// this returns a list of strings
val kids = getKids("john")
// this is will me the returned value
kids = List("amy", "tom")
now I want to insert it:
{
"field1": [
{
"name": "john",
"lname": "knight"
"kids": ["amy", "tom"]
},
{
"name": "jack",
"lname": "samuel"
"kids": ["edi", "keren"]
},
{
"name": "elinor",
"lname": "cooper"
"kids": ["lilly", "mag"]
}
]
...
but I want to iterate on all the names and do this for each one...how can I accomplish this with json4s?
so lets say i have the parsed json:
val myParsedJson = JsonMethods.parse(myJson)
how do I go from here?
thanks!

Related

how to merge multiple json files with same structure into one json file with same structure (combined all into one with keeping same structure))

I need to merge file1.json file2.json (could be more) into onefile.json.
version is always the same value in all files. however vulnerabilities array and dependency_files array values different but there might be duplicate/which I want to remove if any after the merge
file1.json:
{
"version": "x.x.x",
"vulnerabilities": [
{
"id": "0000"
},
{
"id": "11111"
},
{
"id": "2222"
}
],
"dependency_files": [
{
"name": "name0000"
},
{
"name": "name1111"
},
{
"name": "name2222"
}
]
}
file2.json:
{
"version": "x.x.x",
"vulnerabilities": [
{
"id": "2222"
},
{
"id": "3333"
}
],
"dependency_files": [
{
"name": "name2222"
},
{
"name": "name3333"
}
]
}
onefile.json:
{
"version": "x.x.x",
"vulnerabilities": [
{
"id": "0000"
},
{
"id": "11111"
},
{
"id": "2222"
},
{
"id": "3333"
}
],
"dependency_files": [
{
"name": "name0000"
},
{
"name": "name1111"
},
{
"name": "name2222"
},
{
"name": "name3333"
}
]
}
I tried a lot with no luck
You could have a reduce on all files, initialized with the first, hence no need for the -n option:
jq '
reduce inputs as {$vulnerabilities, $dependency_files} (.;
.vulnerabilities = (.vulnerabilities + $vulnerabilities | unique_by(.id))
| .dependency_files = (.dependency_files + $dependency_files | unique_by(.name))
)
' file*.json
{
"version": "x.x.x",
"vulnerabilities": [
{
"id": "0000"
},
{
"id": "11111"
},
{
"id": "2222"
},
{
"id": "3333"
}
],
"dependency_files": [
{
"name": "name0000"
},
{
"name": "name1111"
},
{
"name": "name2222"
},
{
"name": "name3333"
}
]
}
Demo
Using this python code
import json
def merge_dicts(*dicts):
r = {}
skip = 'version'
for item in dicts:
for key, value in item.items():
if (key == skip):
r[skip] = value
else:
r.setdefault(key, []).extend(value)
unique = []
for obj in r[key]:
if obj not in unique:
unique.append(obj)
r[key] = unique
return r
with open("file1.json") as file_1:
data_1 = json.load(file_1)
with open("file2.json") as file_2:
data_2 = json.load(file_2)
with open('data.json', 'w') as merge_file:
json.dump(merge_dicts(data_1, data_2), merge_file, indent = 4)
Result
{
"version": "x.x.x",
"vulnerabilities": [
{
"id": "0000"
},
{
"id": "11111"
},
{
"id": "2222"
},
{
"id": "3333"
}
],
"dependency_files": [
{
"name": "name0000"
},
{
"name": "name1111"
},
{
"name": "name2222"
},
{
"name": "name3333"
}
]
}
This code is multiple json files support
import os, json
def merge_dicts(*dicts):
r = {}
skip = 'version'
for item in dicts:
for key, value in item.items():
if (key == skip):
r[skip] = value
else:
r.setdefault(key, []).extend(value)
unique = []
for obj in r[key]:
if obj not in unique:
unique.append(obj)
r[key] = unique
return r
json_files = [pos_json for pos_json in os.listdir('./') if pos_json.endswith('.json')]
a = []
print(type(a))
for json_file in json_files:
with open(json_file) as file_item:
read_data = json.load(file_item)
a.append(read_data)
file_item.close()
with open('data.json', 'w') as merge_file:
json.dump(merge_dicts(*tuple(a)), merge_file, indent = 4)

building url query string using n1ql

using couchbase 5
I need to build a query string from this object
[
{
"_id": 190,
"querystring": [
{
"name": "p1",
"value": "val1"
},
{
"name": "p2",
"value": "val2"
}
]
}
]
the expected output should be
p1=val1&p2=val2
can anyone help here?
after few attempts I think I got closer to the solution I need.
[
{
"_id": 190,
"res": [
"company_id=$PREFIJO&",
"user_country=$COUNTRY&",
"offer_unique_code=$PIXEL&",
"pub_id=$PUBID&"
]
}
]
now, how can I convert "res" to a concatenated string of all the array elements?
WITH obj AS ({ "_id": 190, "querystring": [ { "name": "p1", "value": "val1" }, { "name": "p2", "value": "val2" } ] })
SELECT obj._id, CONCAT2("&", ARRAY CONCAT2("=",v.name,v.`value`) FOR v IN obj.querystring END) AS res;
Array of objects
WITH objs AS ([{ "_id": 190, "querystring": [ { "name": "p1", "value": "val1" }, { "name": "p2", "value": "val2" } ] },
{ "_id": 191, "querystring": [ { "name": "p3", "value": "val1" }, { "name": "p4", "value": "val2" } ] }
])
SELECT obj._id, CONCAT2("&", ARRAY CONCAT2("=",v.name,v.`value`) FOR v IN obj.querystring END) AS res FROM objs AS obj ;
Older version where CONCAT2() not available, get array of strings (name=val) and do in application or use the following technique. Assume your name/val doesn't have any replace characters.
WITH objs AS ([{ "_id": 190, "querystring": [ { "name": "p1", "value": "val1" }, { "name": "p2", "value": "val2" } ] },
{ "_id": 191, "querystring": [ { "name": "p3", "value": "val1" }, { "name": "p4", "value": "val2" } ] }
])
SELECT obj._id, replace(replace(replace(encode_json(ARRAY CONCAT(v.name,"=",v.`value`) FOR v IN obj.querystring END),"\",\"","&"),"[\"",""),"\"]","") AS res FROM objs AS obj ;
If single document then have ARRAY of objects then use UNNEST
If there is number , convert to string using TO_STR() before CONCAT operation
https://docs.couchbase.com/server/current/n1ql/n1ql-language-reference/stringfun.html#fn-str-concat2

Scala parsing nested json with Json4s

I am trying to fetch data from nested JSON, I need only a few fields from the JSON,
I have created case classes for the required data, the solution I found from google suggested to use read function, but I get an empty Object
I tried to google with no success, What I am missing?
my code
val rawDataFromFile = Source.fromFile(path).mkString
case class Data(listOfPersons: List[Person])
case class Person(bio: Bio, terms: List[Term])
case class Bio(birthday: String, gender: String)
case class Term(`type`: String, start: String, end: String)
read[Data](rawDataFromFile)
res >> Data(List())
and the JSON
[
{
"id": {
"not_intresting_field_1": "B000944",
"not_intresting_field_4": [
"H2OH13033",
"S6OH00163"
]
},
"name": {
"first": "first_name_1",
"last": "last_name_1"
},
"bio": {
"birthday": "1952-11-09",
"gender": "M"
},
"terms": [
{
"type": "rep",
"start": "1993-01-05",
"end": "1995-01-03"
},
{
"type": "rep",
"start": "1995-01-04",
"end": "1997-01-03"
}
]
},
{
"id": {
"not_intresting_field_1": "C000127",
"not_intresting_field_4": [
"S8WA00194",
"H2WA01054"
]
},
"name": {
"first": "first_name_1",
"last": "last_name_1"
},
"bio": {
"birthday": "1958-10-13",
"gender": "F"
},
"terms": [
{
"type": "rep",
"start": "1993-01-05",
"end": "1995-01-03"
},
{
"type": "sen",
"start": "2001-01-03",
"end": "2007-01-03"
}
]
}
]
Your case class is not the same as your json structure.
Here your define Data type which will read json like following
{
"listOfPersons": [
{
"id": {
"not_intresting_field_1": "B000944",
"not_intresting_field_4": [
"H2OH13033",
"S6OH00163"
]
},
"name": {
"first": "first_name_1",
"last": "last_name_1"
},
"bio": {
"birthday": "1952-11-09",
"gender": "M"
},
... //your original json
}
]
}
Try this
read[List[Person]](rawDataFromFile)

Nested dicts and lists / glom lib python

I am trying to access deep-nested lists and dictionaries. I am experimenting with the glom library however my Third_KV key doesn't work on the below JSON object when trying to retrieve the "Country"
from glom import glom
target = {
"Result": {
"Topics": [
{
"A": "abc",
"D": 0,
"Questions": [
{
"E": "jklm",
"P": "dsfs",
"Answers": [
{
"first": "string",
"second": "string",
"Country": "CH"
},
{
"first": "string",
"second": "string",
"Country": "NL"
}
]
}
]
}
]
}
}
path = {
"First_KV": ("Result.Topics", ["Questions"]),
"Second_KV": ("Result.Topics", [("Questions", ["Answers"])]),
"Third_KV": ("Result.Topics", [("Questions", "Answers", ["Country"])])
}
countries = glom(target, path["Third_KV"])
Not very clear what final json/array/structure you want, but without relying on any library, can you not use simple map() e.g.
const jsonTest = {
"Result": {
"Topics": [{
"A": "abc",
"D": 0,
"Questions": [{
"E": "jklm",
"P": "dsfs",
"Answers": [{
"first": "CHfirstCountry",
"second": "CHsecondCountry",
"Country": "CH"
},
{
"first": "NLfirstCountry",
"second": "NLsecondCountry",
"Country": "NL"
}
]
}]
}]
}
};
const AnswersArray = jsonTest.Result.Topics[0].Questions[0].Answers;
let dictPerCountry = new Object();
AnswersArray.map((eachElement) => {
dictPerCountry[eachElement.Country] = [eachElement.first, eachElement.second];
});
console.log({
dictPerCountry
});
dictPerCountry will look like so:
{
"dictPerCountry": {
"CH": [
"CHfirstCountry",
"CHsecondCountry"
],
"NL": [
"NLfirstCountry",
"NLsecondCountry"
]
}
}
Answers are of "list" type too and you are missing its square brackets. check below pattern to get the country
pattern = ('Result.Topics', [('Questions', [('Answers', ['Country'])])])
So you need to change your dictionary "path" to be
path = {
"First_KV": ("Result.Topics", ["Questions"]),
"Second_KV": ("Result.Topics", [("Questions", ["Answers"])]),
"Third_KV": ('Result.Topics', [('Questions', [('Answers', ['Country'])])])
}

Re-arrange JSON file (using adjacency matrix)

I have a json file that looks like this:
[
{
"id": 1,
"country": "Greece",
"names": [
"Alex",
"Betty",
"Catherine",
"Dave",
"Edward",
"Frank",
"George",
"Helen",
"Irene"
]
},
{
"id": 2,
"country": "US",
"names": [
"John",
"Alex",
"Edward",
"Kate",
"Robert",
"Irene",
"Tim",
"Sam"
]
},
{
"id": 3,
"country": "France",
"names": [
"Helen",
"Kate",
"Louise",
"Tim",
"Catherine",
"Arthur",
"Frank",
"Natalie",
"Dave"
]
},
{
"id": 4,
"country": "India",
"names": [
"Ritesh",
"Alex",
"Betty",
"Robert"
]
},
{
"id": 5,
"country": "India",
"names": [
"Nafeez",
"Tom",
"Natalie",
"Gunar",
"Louise",
"Arthur"
]
}
]
I want it to be "name centered" and look like this:
{
"groups": [
{
"gr_id":1
"name":"Alex",
"country":"Greece"
},
.........
{
"gr_id":1
"name":"Irene",
"country":"Greece"
},
{
"gr_id":2
"name":"John",
"country":"US"
..........
{
"gr_id":2
"name":"Sam",
"country":"US"
},
{
"gr_id":3
"name":"Helen",
"country":"France"
},
.........
{
"gr_id":3
"name":"Dave",
"country":"France"
},
{
"gr_id":4
"name":"Ritesh",
"country":"India"
},
........
{
"gr_id":4
"name":"Robert",
"country":"India"
},
{
"gr_id":5
"name":"Nafeez",
"country":"India"
},
...........
{
"gr_id":5
"name":"Arthur",
"country":"India"
}
],
"links": [
{
"source":"Alex"
"target":"Irene",
"count":1
"country":"Greece"
},
...
{
"source":"Alex"
"target":"Arthur",
"count":0
"country":"India"
},
...
]
}
For count in Links I have an adjacency matrix for each country/name (csv format) like this :screenshot of csv file (ad. matrix for India)
This json is just an example. I have much bigger file (I need it for D3 graph visualization)
Reduce() and map() work perfectly for this. This basically takes each item and then maps over the names, appending the results of map() to an array:
let obj = {}
obj.groups = json.reduce(
(acc, curr) => acc.concat(curr.names.map(
item => ({gr_id: curr.id, country: curr.country, name: item})
)), [])
console.log(obj)
// { groups:
// [ { gr_id: 1, country: 'Greece', name: 'Alex' },
// { gr_id: 1, country: 'Greece', name: 'Betty' },
// ...etc
// ]
// }