How to remove field in nested json array by using MySQL functions? - mysql

Lets say I have the following JSON structure in my DB:
{
"user": "test",
"cars": [
{
"name": "car1",
"drivers": [
{
"name": "name1",
"age": 18
},
{
"name": "name2",
"age": 21
}
]
},
{
"name": "car2",
"drivers": [
{
"name": "name3",
"age": 25
}
]
}
]
}
I want to remove all the age fields in the nested arrays?
How can I achieve this via MySQL JSON functions?

Related

Merge Json Array Nodes and roll up a child element

I have a requirement to roll a collection of nodes that uses the current node name (within the collection) and for the value take each child nodes value (single node) into a string array, then use the parents key as the key.
Given.
{
"client": {
"addresses": [
{
"id": "27ef465ef60d2705",
"type": "RegisteredOfficeAddress"
},
{
"id": "b7affb035be3f984",
"type": "PlaceOfBusiness"
},
{
"id": "a8a3bef166141206",
"type": "EmailAddress"
}
],
"links": [
{
"id": "29a9de859e70799e",
"type": "Director",
"name": "Bob the Builder"
},
{
"id": "22493ad4c4fd8ac5",
"type": "Secretary",
"name": "Jennifer"
}
],
"Names": [
{
"id": "53977967eadfffcd",
"type": "EntityName",
"name": "Banjo"
}
]
}
}
from this the output needs to be
{
"client": {
"addresses": [
"RegisteredOfficeAddress",
"PlaceOfBusiness",
"EmailAddress"
],
"links": [
"Director",
"Secretary"
],
"Names": [
"EntityName"
]
}
}
What is the best way to achieve this? Any pointers to what/how to do this would be greatly appreciated.
Ron.
You can iterate over entries of your client object first with the help of the $each function, then get types for each of them, and combine via $merge:
{
"client": client
~> $each(function($list, $key) {{ $key: $list.type }})
~> $merge
}
Live playground: https://stedi.link/OpuRdE9

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)

Getting unique values from nested Array using jq

Trying to get unique values stored in items array for each group. somehow it's always mixed...
My JSON looks like this:
{
"start": 1534425916,
"stop": 1535030716,
"groups": [
{
"group": "transmission",
"data": {
"events": 665762,
},
"items": [
{
"item": "manualni",
"data": {
"events": 389158,
}
},
{
"item": "automaticka",
"data": {
"events": 276604,
}
}
]
},
{
"group": "vat",
"data": {
"events": 671924,
},
"items": [
{
"item": "ne",
"data": {
"events": 346221,
}
},
{
"item": "ano",
"data": {
"events": 325703,
}
}
]
}
]
}
Desired result is the following:
{
"id": "transmission",
"value": [
"manualni",
"automaticka",
]
}
{
"id": "vat",
"value": [
"ne",
"ano"
]
}
Tried with this filter on command line:
| jq '{id: .groups[].group, value: [.groups[].items[].item]}'
Which results in the above mentioned mixed up result:
{
"id": "transmission",
"value": [
"manualni",
"automaticka",
"ne",
"ano"
]
}
{
"id": "vat",
"value": [
"manualni",
"automaticka",
"ne",
"ano"
]
}
Any idea how to receive the uniquified values here? Thanks in advance!
This gets the desired result. I think the manual entry under .[] explains why it works.
jq '.groups[] | {"id": .group, "value": [.items[].item]}'

Add the same element of array in a existing JSON using jq

I have a json file and I want to add some value from top in another place in json.
I am trying to use jq command line.
{
"channel": "mychannel",
"videos": [
{
"id": "10",
"url": "youtube.com"
},
{
"id": "20",
"url": "youtube.com"
}
]
}
The output would be:
{
"channel": "mychannel",
"videos": [
{
"channel": "mychannel",
"id": "10",
"url": "youtube.com"
},
{
"channel": "mychannel",
"id": "20",
"url": "youtube.com"
}
]
}
in my json the "channel" is static, same value always. I need a way to concatenate always in each video array.
Someone can help me?
jq .videos + channel
Use a variable to remember .channel in the later stages of the pipeline.
$ jq '.channel as $ch | .videos[].channel = $ch' tmp.json
{
"channel": "mychannel",
"videos": [
{
"id": "10",
"url": "youtube.com",
"channel": "mychannel"
},
{
"id": "20",
"url": "youtube.com",
"channel": "mychannel"
}
]
}

How to use JSONSlurper to perform assertion on JSON array response in groovy

I'm trying to write extract the name value "Acura" from a JSON array response by using the location of the value stored in a variable called "jsonFieldName".
Below is the code that I'm trying to do this with, however, everytime i run the script, SOAPUI returns error: "java.lang.NullPointerException: Cannot get property 'name' on null object error at line: 156"
Can someone kindly advise how to do this?
import groovy.json.JsonSlurper
def response = '''{
"makes": [
{
"id": 200002038,
"name": "Acura",
"niceName": "acura",
"models": [
{
"id": "Acura_ILX",
"name": "ILX",
"niceName": "ilx",
"years": [
{
"id": 200471908,
"year": 2014
}
]
},
{
"id": "Acura_ILX_Hybrid",
"name": "ILX Hybrid",
"niceName": "ilx-hybrid",
"years": [
{
"id": 200493809,
"year": 2014
}
]
},
{
"id": "Acura_MDX",
"name": "MDX",
"niceName": "mdx",
"years": [
{
"id": 200465929,
"year": 2014
}
]
},
{
"id": "Acura_RDX",
"name": "RDX",
"niceName": "rdx",
"years": [
{
"id": 200467168,
"year": 2014
}
]
},
{
"id": "Acura_RLX",
"name": "RLX",
"niceName": "rlx",
"years": [
{
"id": 100539511,
"year": 2014
}
]
},
{
"id": "Acura_TL",
"name": "TL",
"niceName": "tl",
"years": [
{
"id": 200488448,
"year": 2014
}
]
},
{
"id": "Acura_TSX",
"name": "TSX",
"niceName": "tsx",
"years": [
{
"id": 200490517,
"year": 2014
}
]
},
{
"id": "Acura_TSX_Sport_Wagon",
"name": "TSX Sport Wagon",
"niceName": "tsx-sport-wagon",
"years": [
{
"id": 200673755,
"year": 2014
}
]
}
]
},
{
"id": 200001769,
"name": "Aston Martin",
"niceName": "aston-martin",
"models": [
{
"id": "Aston_Martin_DB9",
"name": "DB9",
"niceName": "db9",
"years": [
{
"id": 200473436,
"year": 2014
}
]
},
{
"id": "Aston_Martin_Rapide_S",
"name": "Rapide S",
"niceName": "rapide-s",
"years": [
{
"id": 200460643,
"year": 2014
}
]
},
{
"id": "Aston_Martin_V8_Vantage",
"name": "V8 Vantage",
"niceName": "v8-vantage",
"years": [
{
"id": 200472947,
"year": 2014
}
]
},
{
"id": "Aston_Martin_Vanquish",
"name": "Vanquish",
"niceName": "vanquish",
"years": [
{
"id": 200431313,
"year": 2014
}
]
}
]
}
],
"makesCount": 2
}'''
def jsonFieldName = ('makes[0].name')
def json = new JsonSlurper().parseText (response)
jsonFieldName.split("\\.").each{json = json[it]}
assert json == 'Acura'
Assuming that your JSON is good from the response (check by calling print) Try adding .text to the end of your jsonSlurper() Call
It also looks like you have a space between parseText and (response)
so it should be
def json = new JsonSlurper().parseText(response)
However I would try casting to ArrayList<LazyMap> to ensure you can iterate by
ArrayList<LazyMap> json = new JsonSlurper().parseText(response) as ArrayList<LazyMap>
Then call :
json.get('Acura')
This line of your code doesn't handle the index resolution:
jsonFieldName.split("\\.").each{json = json[it]}
There is no key with the name makes[0]. Instead there is an array of makes and you are interested on the first one. The following hardcoded line retrieves the name attribute:
def result = json.'makes'[0].'name'
As you can see here there is an additional step to resolve the index operator. Of course you can implement this functionality on your own or use JsonPath instead of JsonSlurper.
OK so I managed to get this working by using JsonPath instead of JsonSlurper.
To achieve this I had to import the following:
import com.jayway.jsonpath.JsonPath
def jsonFieldName = "makes[0].name"
def expectedValue = "Acura"
def jsonSuff = JsonPath.read(response, jsonFieldName)
log.info(jsonSuff)
if (jsonSuff.toString() == expectedValue.toString()){
log.info("Actual value"+jsonSuff+"is equal to expected value"+expectedValue)
}