Cannot get array property by using Parse JSON groovy - json

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?

Related

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)

Unable to parse Service-Now JSON Data

I am new to Python/Scala. Trying to read/parse a JSON file from dataLake using Python and Scala but unsuccessful. Is there something wrong with the .JSON schema. I have attached the JSON schema and the data frame screenshots. Any help would be highly appreciated.
{
"result": [
{
"instance": {
"link": "https://service-now.com/api/now/table/assessment_instance/3d2a2c44d720058ba859e03d2",
"value": "3d2a2c44d720058ba859e03d2"
},
"string_value": "Very Good",
"assign_to_order": "",
"instance_question": {
"link": "https://service-now.com/api/now/table/assessment_instance_question/022a2c44d720058ba859e03d7",
"value": "022a2c44d720058ba859e03d7"
},
"template_definition": "",
"metric_definition": {
"link": "https://service-now.com/api/now/table/metric_definition/7758eb387233200dbba82cb0bfb",
"value": "7758eb387233200dbba82cb0bfb"
},
"sys_updated_on": "207-07-0 02:3:3",
"scaled_value": "4",
"metric": {
"link": "https://service-now.com/api/now/table/metric/542a2c44d720058ba859e03a4",
"value": "542a2c44d720058ba859e03a4"
},
"sys_created_on": "203-2-04 2:34:08",
"sys_domain": {
"link": "https://service-now.com/api/now/table/sys_user_group/global",
"value": "global"
},
"weighted_value": "0.8",
"source_id": {
"link": "https://service-now.com/api/now/table/type/502a2c44d720",
"value": "502a2c44d720058ba859e03a3"
},
"normalized_value": ".33",
"source_table": "metric_type",
"user": "",
"sys_created_by": "admin"
}
]
}
val asmt_metric_result = spark.read.option("multiline", "true")
.json(s"c:/servicenow/asmt_metric_result/2021/09/28/08/32/asmt_metric_result_000000.json")
asmt_metric_result.show(false)

How to map Nested Array of Objects using Swift Object Mapper?

I am trying to map an array of objects using Object Mapper
I have this code so far, and my mapping is not successful
do {
if let data = data, let sectorData = Mapper<SubSectorsModel>().mapArrayOfArrays(JSONObject: try JSONSerialization.data(withJSONObject: data, options: [])) {
completionHandler(sectorData,(response as! HTTPURLResponse), error)
print("SectionData Received Successfully")
}
} catch {
completionHandler(nil,(response as! HTTPURLResponse), error)
print("Error parsing json get sector data: ", error.localizedDescription)
}
My Json data is as follows:
[
{
"SECTOR_NAME": "MANUFACTURERS",
"ID": "8",
"SECTOR": [
{
"ID": "144",
"NAME": "Biomass Processing"
},
{
"ID": "8",
"NAME": "Servicing engines and motors"
},
{
"ID": "23",
"NAME": "Furniture & fittings"
},
{
"ID": "31",
"NAME": "Fabrics & textiles"
},
{
"ID": "20",
"NAME": "Hand and machine tools"
},
{
"ID": "28",
"NAME": "Safety and security products"
},
{
"ID": "147",
"NAME": "Jewellery"
},
{
"ID": "156",
"NAME": "Beverages"
},
{
"ID": "165",
"NAME": "Stationery"
},
{
"ID": "9",
"NAME": "Industrial equipment"
},
{
"ID": "25",
"NAME": "Cleaning equipment"
},
{
"ID": "33",
"NAME": "Household consumer products"
},
{
"ID": "162",
"NAME": "Paper Products"
},
{
"ID": "170",
"NAME": "Memoribilia"
},
{
"ID": "143",
"NAME": "Food Products"
},
{
"ID": "22",
"NAME": "Automotive aviation marine and rail products"
},
{
"ID": "30",
"NAME": "Household appliances"
},
{
"ID": "151",
"NAME": "Iron Sheet"
},
{
"ID": "167",
"NAME": "Cosmetics"
},
{
"ID": "11",
"NAME": "Fuel Lubricants & Detergents"
},
{
"ID": "19",
"NAME": "Electrical appliances and equipment"
},
{
"ID": "27",
"NAME": "Packaging products"
},
{
"ID": "7",
"NAME": "Engines & parts"
},
{
"ID": "24",
"NAME": "Glass products"
},
{
"ID": "32",
"NAME": "Clothing & footwear"
},
{
"ID": "152",
"NAME": "Building Material"
},
{
"ID": "142",
"NAME": "Food Processing and Packaging"
},
{
"ID": "21",
"NAME": "Plastic products"
},
{
"ID": "29",
"NAME": "Pool & garden products"
},
{
"ID": "157",
"NAME": "Steel Products"
},
{
"ID": "138",
"NAME": "Optical Prescription Lenses"
},
{
"ID": "10",
"NAME": "Servicing & refurbishing"
},
{
"ID": "18",
"NAME": "Chemical"
},
{
"ID": "26",
"NAME": "Board paper and"
}
]
},
.
.
.
]
If you just want to send completionHandler anyway, use code below:
struct MyJsonStruct : Decodable {
struct SectorStruct : Decodable {
var ID : String
var NAME : String
}
var SECTOR_NAME : String
var ID : String
var SECTOR : [SectorStruct]
}
func handle(_ data : Data ) {
do {
let sectorData = try JSONDecoder().decode(MyJsonStruct.self, from: data) as MyJsonStruct
let yourArray = sectorData.SECTOR // if you need an array result
completionHandler(sectorData,(response as! HTTPURLResponse), error)
print("SectionData Received Successfully")
} catch {
completionHandler(nil,(response as! HTTPURLResponse), error)
print("Error parsing json get sector data: ", error.localizedDescription)
}
}

get a node value from json file

I am working on a facebook graph api project and have this json file:
{
"data": [
{
"id": "407356746023254_569534706472123",
"from": {
"category": "Furniture",
"name": "`enter code here`",
"id": "407356746023254"
},
"message": "message",
"actions": [
{
"name": "Comment",
"link": "https://www.facebook.com/407356746023254/posts/569534706472123"
},
{
"name": "Like",
"link": "https://www.facebook.com/407356746023254/posts/569534706472123"
}
],
"privacy": {
"value": ""
},
"type": "photo",
"status_type": "added_photos",
"object_id": "569534676472126",
"created_time": "2013-12-03T22:54:45+0000",
"updated_time": "2013-12-04T15:29:03+0000",
"shares": {
"count": 1
},
I am working with Newtonsoft.Json.Linq to parse this file
my code:
For i = 0 To o("data").Count - 1
Dim o As JObject = JObject.Parse(data)
Response.Write(o("data")(i)("shares"))
next
the code result is {"count": 1 }
I want to get only the value "1" from
"shares": {
"count": 1
},
but I can't
I think I missed the concept in someway . Any body can help?
For i = 0 To o("data").Count - 1
Dim o As JObject = JObject.Parse(data)
Response.Write(o("data")(i)("shares")("count"))
next
This may help you... :) ;) :P

json parsererror

why this error
xhr: [object XMLHttpRequest]
textStatus: parsererror
errorThrown: Invalid JSON: [ { "id": "10000000", "label": "10000000" }, { "id": "10000001", "label": "10000001" }, { "id": "10000003", "label": "10000003" }, { "id": "10000005", "label": "10000005" }, { "id": "10000006", "label": "10000006" } ]
Copied and pasted this:
[ { "id": "10000000", "label": "10000000" }, { "id": "10000001", "label": "10000001" }, { "id": "10000003", "label": "10000003" }, { "id": "10000005", "label": "10000005" }, { "id": "10000006", "label": "10000006" }]
to JSONlint and it says it's valid.
Can you post the original JSON if that is the original then the it is missing a root key
var textStatus = {"keys":[{"id":"...", "label":"..."},{"id":"...","label":"..."}]}
Also the whole thing needs to be inside {} to make it an object what you had is just an array