Deep_search in big json object by method ruby - json

I have a problem with filtering JSON object in ruby!
1. My JSON object is a big array of two hashes.
2. That hashes includes another hashes that include another arrays and hashes (oh god! :c).
My goal is to output Big hash that contains concrete value!
Examples down below:
JSON file just like in
[#That's hash 0{
"id": 0,
"firstName": "",
"lastName": "",
"middleName": null,
"email": "",
"phones": [
null,
null
],
"groups": [{
"id": 0,
"name": ""
}],
"disabled": "",
"technologies": [{
"id": 0,
"name": "",
"children": [{
"id": 1,
"name": "",
"children": [{
"id": 2,
"name": "Farmer",
"children": []
}]
}]
}],
"fullName": ""
},
#That's hash1{
"id": 0,
"firstName": "",
"lastName": "",
"middleName": null,
"email": "",
"phones": [
null,
null
],
"groups": [{
"id": 0,
"name": ""
}],
"disabled": "",
"technologies": [{
"id": 0,
"name": "",
"children": [{
"id": 1,
"name": "",
"children": [{
"id": 2,
"name": "Not Farmer",
"children": []
}]
}]
}],
"fullName": ""
}
]
Pseudocode on ruby (what I want to):
file = File.read("example.json") #=> Reading JSON file
data_hash = JSON.parse(file, object_class: Hash) #=> Parsing JSON file
data = data_hash.filter #=> filter that hash if "technologies" is not empty!
data.get_hash_by_value(value) #=> For example i put "Not Farmer" in value, and that method must search in all data that (value) and output hash1 for me (because hash0 not include "Not Farmer")
That's big problem, i don't know what to do!!!
My thoughts is a recursive finding method..

I wrote my own functions. Maybe it can help someone.
def check_children(item)
return true if item["name"] == "Farmer"
item["children"].each do |child_item|
break if check_children(child_item)
end
return false
end
data_hash.each do |item|
next if item["technologies"].empty?
item["technologies"].each do |technologies_item|
next if technologies_item["children"].empty?
technologies_item["children"].each do |children_item|
data << item if check_children(children_item)
end
end
end

Related

Add a new element/field to every JSON array/record in an existing JSON file - python 3

I have JSON files that consists of 10+ records, like the one below.
Example:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
}
]
I want to add a new element "Date": "xx/xx/xx" to every record, like this:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
"Date": "xx/xx/xx"
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
"Date": "xx/xx/xx"
}
]
The order does not matter.
So far I have something like this:
date = 'xx-xx-xx'
def file(data, json):
with pathlib.Path(json).open('r+') as upfile:
fdata = json.load(upfile)
fdata.append(data)
upfile.seek(0)
json.dump(fdata, updfile)
...
new_ele = {'Date: ': date}
file(new_ele, file.json)
But my result is like this:
[
{
"ID": null,
"entity": "xxx",
"Name": "Abc",
},
{
"ID": null,
"entity": "yyy",
"Name": "efg",
},
{
"Date": "xx-xx-xx"
}
]
How can I get the Date into every single record?
Thank you
Once imported, your json is a list of dictionaries. So go through all list elements (which are dictionaries) and update them with the update method:
def update_json(data, json_path):
with pathlib.Path(json_path).open('r+') as upfile:
fdata = json.load(upfile)
for elt in fdata:
elt.update(data)
upfile.seek(0)
json.dump(fdata, upfile)

Parsing Git Json with Regular Express

I am taking a Github json file and parsing it with Java's regular expression library JsonPath. I am having a problem parsing arrays that do not have labels.
I need to send a email every time a particular file is changed in our repository.
Here is the Git Json:
{
"trigger": "push",
"payload": {
"type": "GitPush",
"before": "xxxxxxxx",
"after": "yyyyyyyy",
"branch": "branch-name",
"ref": "refs/heads/branch-name",
"repository": {
"id": 42,
"name": "repo",
"title": "repo",
"type": "GitRepository"
},
"beanstalk_user": {
"type": "Owner",
"id": 42,
"login": "username",
"email": "user#example.org",
"name": "Name Surname"
},
"commits": [
{
"type": "GitCommit",
"id": "ffffffff",
"message": "Important changes.",
"branch": "branch-name",
"author": {
"name": "Name Surname",
"email": "user#example.org"
},
"beanstalk_user": {
"type": "Owner",
"id": 42,
"login": "username",
"email": "user#example.org",
"name": "Name Surname"
},
"changed_files": {
"added": [
"NEWFILE",
],
"deleted": [
"Gemfile",
"NEWFILE"
],
"modified": [
"README.md",
"NEWFILE"
],
"copied": [
]
},
"changeset_url": "https://subdomain.github.com/repository-name/changesets/ffffffff",
"committed_at": "2014/08/18 13:30:29 +0000",
"parents": [
"afafafaf"
]
}
]
}
}
This is the expression I am using: to get the commits
$..changed_files
This return the whole changed files part but I can not explicitly choose the name "NEWFILE"
I tried
$..changed_files.*[?(#.added == "NEWFILE")]
$..changed_files.*[?(#.*== "NEWFILE")]
It just returns a empty array.
I just want it to return Newfile and what type of change. Any Ideas?
You can use the following JsonPath to retrieve the commits which list "NEWFILE" as an added file :
$.payload.commits[?(#.changed_files.added.indexOf("NEWFILE") != -1)]

How to format json output in pyspark?

I am having a trouble to preserve the order of my json and pretty printing it in pyspark.
Below is sample code:
json_out = sqlContext.jsonRDD(sc.parallelize([json.dumps(info)]))
# here info is my ordered dictionary
json_out.toJSON().saveAsTextFile("file:///home//XXX//samplejson")
One more thing is that I want my output as single file and not as partitioned datasets.
Could anyone help in pretty printing and preserving the order of output json in my case?
info sample:
Note:TypeA,TypeB etc is a list meaning there can be more than one product in TypeA or TypeB.
{
"score": {
"right": ,
"wrong":
},
"articles": {
"TypeA": [{
"ID": 333,
"Name": "",
"S1": "",
"S2": "",
"S3": "",
"S4": ""
}],
"TypeB": [{
"ID": 123,
"Name": "",
"T1": "",
"T2": "",
"T3": "",
"T4": "",
"T5": "",
"T6": ""
}]
}
}
( I have tried using json.dumps(info,indent=2),but of no use.

D3 - tree nodes not built from external JSON file

I'm having difficulty processing external JSON data to build/dispaly a tree. The issue is that nodes are not built from received JSON file. The code only looks as at the JSON data as one object and does not create leafs(other nodes)
When adding the same JSON data as array inside HTML file, the same code creates nodes and displays the tree correctly.
Here is an extract of the code in question:
<body onload="myFunction()">
<script type="text/javascript" >
function myFunction()
{
d3.json("/data/mydata.json", function (error, data) {
var root2 = [data];
root = root2[0];
........... other stuff comes here .....
var nodes = tree.nodes(root.reverse());
The browser debugger gives me the following:
nodes
[Array[1]0: Object
childern: Array[2]
name: "Max"
parent: "null"
proto: Object
depth: 0
length: 1
x: 230
y: 0
proto: Array[0]
JOSN file is at the bottom
I appreciate your feedback in this regard.
[{
"name": "Max",
"parent": "null",
"size": 4938,
"childern": [
{
"name": "David",
"parent": "Max",
"Childern": [
{
"name": "Craig",
"parent": "David",
"size": 3938
},
{
"name": "Robin",
"parent": "David",
"size": 3812
},
{
"name": "Anna",
"parent": "David",
"size": 743
}
]
},
{
"name": "Peter",
"parent": "Max",
"Childern": [
{
"name": "Jeff",
"parent": "Peter",
"size": 3534
},
{
"name": "Buffy",
"parent": "Peter",
"size": 5731
}
]
}
]
}
]
Your external JSON file appears to have the 'children' element with a capital 'C' for the lower tiers of data, but with lower case 'c' at the top tier.
Additionally they were misspelled as 'Childern' which may have been causing problems (you will want to check your JavaScript to see how you spelled it there and make it consistent).
I was able to do a quick experiment with an external JSON file and I can confirm that you will get a problem similar to what you describe. So use lower case characters (or at least consistent characters and spelling) for the word 'children' in your data.
Try with the data per below;
[{
"name": "Max",
"parent": "null",
"size": 4938,
"children": [
{
"name": "David",
"parent": "Max",
"children": [
{
"name": "Craig",
"parent": "David",
"size": 3938
},
{
"name": "Robin",
"parent": "David",
"size": 3812
},
{
"name": "Anna",
"parent": "David",
"size": 743
}
]
},
{
"name": "Peter",
"parent": "Max",
"children": [
{
"name": "Jeff",
"parent": "Peter",
"size": 3534
},
{
"name": "Buffy",
"parent": "Peter",
"size": 5731
}
]
}
]
}

Jmeter JSON Path Assertion

Below is my JSON response data, I need to do assertion using the below response. I tried in many ways to write JSON path & Expected Value. Always assertion fails. What I want is please help to write the path and expected value for the below data
{
"statusCode": 200,
"statusMessage": "Success",
"errorMessage": "",
"responseData": {
"id": 15,
"userName": "partner#tarento.com",
"firstName": "tarento",
"lastName": "test1",
"phoneNumber": "1234567812",
"email": "partner#tarento.com",
"password": "",
"city": "",
"agentList": [
{
"id": 37,
"userName": "Rahul.antonyRaj#tarento.com",
"firstName": "Sanjay",
"lastName": "rahul",
"phoneNumber": "7411269480",
"email": "Rahul.antonyRaj#tarento.com",
"password": "",
"active": true
},
{
"id": 68,
"userName": "jinesh.sumedhan#tareto.com",
"firstName": "jinesh",
"lastName": "sumedhan",
"phoneNumber": "9400993826",
"email": "jinesh.sumedhan#tareto.com",
"password": "",
"active": true
},
{
"id": 108,
"userName": "a.sanjayrahul#gmail.com",
"firstName": "Rahul",
"lastName": "Antony",
"phoneNumber": "9994590241",
"email": "a.sanjayrahul#gmail.com",
"password": "",
"active": true
},
{
"id": 304,
"userName": "a.sanjayrajish#gmail.com",
"firstName": "Agent",
"lastName": "Agent",
"phoneNumber": "9025699716",
"email": "a.sanjayrajish#gmail.com",
"password": "",
"active": true
}
],
"roleName": "admin",
"sessionKey": "435tnerLt9813942160478oDse46345635#1",
"partner": {
"id": 1,
"name": "Tarento",
"cityList": [
"bangalore",
"mumbai"
],
"phone": "1234567812",
"url": ""
},
"isActive": true,
"isDeleted": false,
"roleId": 1,
"countryCode": "",
"tags": [
{
"tagId": 1,
"name": "all",
"description": "this is default tag of all driver."
},
{
"tagId": 2,
"name": "airport",
"description": ""
},
{
"tagId": 3,
"name": "street",
"description": "any text message"
},
{
"tagId": 255,
"name": "night",
"description": "night"
}
]
}
}
I received the following response
For start following JSONPath Assertion will test your statusCode
$.statusCode
put 200 to Expected Value of JSONPath Assertion.
This one is for userName
$.responseData.userName
Easy, isn't it? See Parsing JSON guide for more useful examples and how-tos.
I found the JSR223 Assertion with script language javascript to be the easiest. at least if you have knowledge in java and javascript. And no need to add any plugins.
My working code in detail:
var json = JSON.parse(SampleResult.getResponseDataAsString());
if (json.statusCode != 200) {
AssertionResult.setFailureMessage(""
+ json.statusCode
+ " " + json.statusMessage
+ " " + json.errorMessage);
AssertionResult.setFailure(true);
}
I personally prefer to use BSF PostProcessor in coupling with Groovy language. Example of how to parse JSON with Groovy you can find here how to parse json using groovy