I have a json output that I have been able to parse successfully but I'm not able to read some of the properties value using groovy
From the below json data, I can parse this data
def parsedJsonGet = new groovy.json.JsonSlurper().parseText(Response)
def i = -1
parsedJsonGet.each {
thisRecord ->
i= i+1
//Here using thisRecord can go through each node
}
From the below json data I would like to read say Street value of each such nodes.
Json Code is in this format:
{
[
"Name": "ABC",
"Address": {
"":0,
"City": [
{
"Street": "Data1",
"Apt": "Data2",
"Pin": "Data3",
}
]
}
},
{
[
"Name": "ABC",
"Address": {
"":0,
"City": [
{
"Street": "",
"Apt": "",
"Pin": "",
}
]
}
}
Making a few assumptions about your payload, which isn't well-formed JSON, you could parse the data this way:
def Response = '''{
"Records": [
{
"Name": "ABC",
"Address": {
"Number": 0,
"City": [
{
"Street": "Data1",
"Apt": "Data2",
"Pin": "Data3"
}
]
}
}
]
} '''
def parsedJsonGet = new groovy.json.JsonSlurper().parseText(Response)
parsedJsonGet.Records.each {
thisRecord -> System.out.println('Street is ' + thisRecord.Address.City[0].Street)
}
Because City is an array, I'm assuming you want just the first City/Street record. This will return: This record is Data1
Related
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'])])])
}
'[
{
"match_id": "386491",
"match_hometeam_score": "3",
"match_awayteam_score": "1",
"statistics": [
{
"type": "Shots on Goal",
"home": "5",
"away": "5"
}
]
},
{
"match_id": "386495",
"match_hometeam_score": "1",
"match_awayteam_score": "2",
"statistics": [
{
"type": "Shots on Goal",
"home": "10",
"away": "7"
}
]
}
]'
I would like to flatten this json response. I had difficulties doing it since statistics is a list ob objects. Could you please help me with this?
So far I tried;
events_request = requests.get(events_url + api_key)
data = events_request.json()
data
df = pd.json_normalize(data,
sep="_",
record_path=[["statistics"]],
)
Then I tried,
json.dumps(data)
But what I had was not relevant to my expectation. What I expected is a result similar to a csv like this;
match_id,match_hometeam_score,match_awayteam_score,stattype,stathome,stataway
0,386491,3,1,Shots on goal,5,5
1,386495,1,2,Shots on goal,10,7
To get required structure, you can use this script (it will save the data to data.csv):
import json
import pandas as pd
data = '''[
{
"match_id": "386491",
"match_hometeam_score": "3",
"match_awayteam_score": "1",
"statistics": [
{
"type": "Shots on Goal",
"home": "5",
"away": "5"
}
]
},
{
"match_id": "386495",
"match_hometeam_score": "1",
"match_awayteam_score": "2",
"statistics": [
{
"type": "Shots on Goal",
"home": "10",
"away": "7"
}
]
}
]'''
data = json.loads(data)
df = pd.json_normalize(data, 'statistics', ['match_id', 'match_hometeam_score', 'match_awayteam_score'])
df.to_csv('data.csv')
The content of data.csv:
,type,home,away,match_id,match_hometeam_score,match_awayteam_score
0,Shots on Goal,5,5,386491,3,1
1,Shots on Goal,10,7,386495,1,2
Screenshot from LibreOffice:
I'm new to Groovy. But, I can try with some simple code lines to extract the data.
I have read the from Parse JSON using groovy script (using JsonSlurper). But it didn't help in my case.
My code line:
{
"errors": false,
"address_data": [
{
"address_id": "567",
"township": {
"id": "41079",
"name": "Test Data"
},
"city": {
"id": "1622",
"name": "Test City"
},
"region": {
"id": "663",
"name": "Metro Test"
},
"stock_source_code": "Test",
"is_default_address": false
},
{
"address_id": "45444",
"township": {
"id": "41079",
"name": "Test Test"
},
"city": {
"id": "1622",
"name": "Test City"
},
"region": {
"id": "663",
"name": "Metro Test Taguig"
},
"is_default_address": true
},
{
"address_id": "45444",
"township": {
"id": "888888",
"name": "Apas"
},
"city": {
"id": "432",
"name": "Test City"
},
"region": {
"id": "591",
"name": "Test Cebu"
},
"stock_source_code": "testce",
"is_default_address": false
}
]
}
My code lines:
def response = "data_above";
def object = new JsonSlurper().parseText(response);
def errors = object.errors
if (errors == false) { //could access object.errors property
log.info "Checking condition"
def addressData = object.address_data // Cannot get the data
}
How can I get the address_data property?
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!
I've got an MVC 3 web app and am returning a JSON object which I would like to use Linq against to limit the result returned to the client jquery.
The Json response takes the following structure:
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
},
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
....
I need to return or grab just the json items that have a Name like 'Setting' for example. In the example, this would return just the 2 Json nodes.
My Linq is very limited and what I have is: Settings is where the Json response is stored.
NewtonSoft.Json.Linq.JObject data = NewtonSoft.Json.Linq.JObject.Parse(Settings);
var result = from p in data["Data"]["Items"].Children()
where (p["Result"]["Name"].Contains("Expenses.Help.Tip"))
select new { Name = (string)p["Result"]["Name"], Value = (string)p["Result"]["Value"] };
When I run this I get nothing in my result. Can anyone help and tell me what I'm doing wrong?
Thanks.
Well, I'm not a Json specialist, but I think your Json structure has some problems.
I tried an online parser, and parsing took only the third result... (try to copy past your code in the left window, and look at JS eval.
Instead of
"Items": [
{
"Result": {
},
"Result": {
},
"Result": {
}
}]
you should have each element of Items array (each 'Result') into {}.
"Items": [
{
{"Result": {
}
},
{"Result": {
}
},
{"Result": {
}
}]
I got it to work by changing your Json file to
{
"Data": {
"Items": [
{
"Result": {
"Id": "e2ba4912-c387-4f54-b55e-06742a6858db",
"Name": "SomeOtherSetting",
"Value": "500",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting2",
"Value": "600",
"Archived": false
}
},
{
"Result": {
"Id": "17c27584-cea8-42c2-b6c4-0b30625ac3ce",
"Name": "Setting3",
"Value": "700",
"Archived": false
}
}]
}
}
using
var data = JObject.Parse(test)["Data"]["Items"].Children()
.Where(m => m["Result"]["Name"].Value<string>().Contains("Setting"))
.Select(m => new
{
Name = m["Result"]["Name"].Value<string>(),
Value = m["Result"]["Value"].Value<int>()
})
.ToList();