creating JSON object without using a list - json

I want to be able to create a JSON object so that I can access it like this.
education.schools.UNCC.graduation
Currently, my JSON is like this:
var education = {
"schools": [
"UNCC": {
"graduation": 2015,
"city": "Charlotte, NC",
"major": ["CS", "Spanish"]
},
"UNC-CH": {
"graduation": 2012,
"city": "Chapel Hill, NC"
"major": ["Sociology", "Film"]
}
],
"online": {
"website": "Udacity",
"courses": ["python", "java", "data science"]
}
};
When I go to Lint my JSON, I get an error message.
I know I can reformat my object to access it like this (below), but I don't want to do it this way. I want to be able to call the school name, and not use an index number.
education.schools[1].graduation

Objects have named keys. Arrays are a list of members.
Replace the value of "schools" with an object. Change [] to {}.

This is your JSON corrected.
Your JSON is invalid.
{
"schools": [
{
"UNCC": {
"graduation": "2015",
"city": [
"CS",
"Spanish"
],
"major": [
"CS",
"Spanish"
]
}
},
{
"UNC-CH": {
"graduation": "2012",
"city": [
"Chapel Hill",
"NC"
],
"major": [
"Sociology",
"Film"
]
}
}
],
"online": {
"website": "Udacity",
"courses": [
"python",
"java",
"data science"
]
}
}
Explanation:
"city": "Chapel Hill, NC" -> this is a array with 2 values "Chapel Hill" and "HC", like you do with major and courses.
The Schools array, you need to use this sintaxe to construct a array [{
http://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html

Related

ADF Data Flow Remove empty JSON objects and arrays when building JSON files

I am using a Data Flow in Azure Data Factory to transform rows of CSV files into JSON documents of a standard layout.
I can't figure out how to get rid of empty JSON objects and arrays when there is no data to populate.
For example if I have a CSV like below:
firstName,lastName,Email,Address1,City,State,Zip
Bob,Smith,someemail#email.com,123 st,Somecity,TX,12345
I need to turn it into this:
{
"firstName": "Bob",
"lastName": "Smith",
"contactData": [
{
"contactType": "postalAddress",
"contactData": {
"postalAddress1": "123 st",
"postalCity": "Somecity",
"postalState": "TX",
"postalCode": "12345"
}
},
{
"contactType": "email",
"contactData": {
"emailAddress": "someemail#email.com"
}
}
]
}
I am using derived columns to build the subcolumns and arrays. I have been able to produce the JSON above.
The problem I run into is that if an email or address is null, I want to remove the object from the array.
If both are null, I want to remove the entire contactData object.
Example:
firstName,lastName,Email,Address1,City,State,Zip
Bob,Smith,,,,,
I need to turn it into this:
{
"firstName": "Bob",
"lastName": "Smith"
}
If I set all of the child objects to NULL with IF statements I can produce something like this:
{
"firstName": "Bob",
"lastName": "Smith",
"contactData": [
{
"contactData": {}
},
{
"contactData": {}
}
]
}
but I can't get rid of the entire section.
The Sink will get rid of the empty string objects, but not the nested JSON objects and arrays.
Is there any way to do this in ADF Data Flows?
You can split the data and then apply union to get the desired result.
Since I don't have the previous data transformations, I have taken the following data as my source data.
[{
"firstName": "Bob",
"lastName": "Smith",
"contactData": [
{
"contactType": "postalAddress",
"contactData": {
"postalAddress1": "123 st",
"postalCity": "Somecity",
"postalState": "TX",
"postalCode": "12345"
}
},
{
"contactType": "email",
"contactData": {
"emailAddress": "someemail#email.com"
}
}
]
},
{
"firstName": "b1",
"lastName": "s1",
"contactData": [
{
"contactData": {}
},
{
"contactData": {}
}
]
},
{
"firstName": "Bob1",
"lastName": "Smith1",
"contactData": [
{
"contactType": "postalAddress",
"contactData": {
"postalAddress1": "123 st1",
"postalCity": "Somecity1",
"postalState": "TX1",
"postalCode": "123456"
}
},
{
"contactType": "email",
"contactData": {
"emailAddress": "someemail1#email.com"
}
}
]
},
{
"firstName": "b2",
"lastName": "s2",
"contactData": [
{
"contactData": {}
},
{
"contactData": {}
}
]
}]
Now, I have taken a sample derived column to find the length of contactType. The rows without any contactType would have the same length (convert to string and find length).
tp : length(toString(contactData.contactType))
So, split the data based on whether the contactType (converted to string) length is equal to 2 or not. The split condition would be as shown below:
length(toString(contactData.contactType))!=2
This will split the data as required. The noContact stream would have the following data:
Now select only required columns. I have used rule based selection to select only columns where name is not contactData.
condition : name!='contactData'
column name : $$
Now apply union transformation by Name on hasContact and select1 stream.
I have configured the sink dataset as shown in the image below:
In sink, output to a single JSON file with selected name (under settings tab). The data preview in sink would be as:
Once the file is written, the null fields would not be written, so the data would be as per requirement. The following is an image for reference.

To extract specific key into a list from JSON

How do i extract the values of terms in below JSON into a list in opposite order. In this example i want the output to be ["Auto Ontology","hdfc bank","giftplus card"].
{
"faqs": [
{
"question": "Can the value of the HDFC Bank Giftplus card be topped up?",
"alternateQuestions": [
],
"terms": [
"giftplus card",
"hdfc bank",
"Auto Ontology"
],
"tags": [
],
"refId": "7aea18a1-32c8-5af4-8d77-da31f9c7ff26",
"responseType": "message",
"answer": [
{
"text": "Gifting we believe is special to the occasion and hence you cannot reload this card. Every occasion is a new occasion and deserves a new card.",
"type": "basic",
"channel": "default"
}
],
"alternateAnswers": [
]
},**strong text**
{
"faqs": [
{
"question": "Can the value of the HDFC Bank Giftplus card be topped up?",
"alternateQuestions": [
],
"terms": [
"giftplus card",
"hdfc bank",
"Auto Ontology"
],
"tags": [
],
"refId": "7aea18a1-32c8-5af4-8d77-da31f9c7ff26",
"responseType": "message",
"answer": [
{
"text": "Gifting we believe is special to the occasion and hence you cannot reload this card. Every occasion is a new occasion and deserves a new card.",
"type": "basic",
"channel": "default"
}
],
"alternateAnswers": [
]
}
]
}
You can use the python code given below To print opposite order
import json
f = open('rev.json', 'r')
a = json.load(f)
b = a["faqs"][0]["terms"][::-1]
print(b)
Output:
['Auto Ontology', 'hdfc bank', 'giftplus card']
You may try to use the snippet
Var = a["faqs"][0]["terms"][::-1]
The ::-1 is used to reverse the output.

match results of SQL Query with JSON result from Invoke-RestMethod

I have two JSON files that i need to match for similar data and return only the matching data. 1 file is a result of a SQL query and another is a result of a Invoke-RestMethod Get request
i have tried to store the different json data into arrays but i am able to drill down properly into the JSON objects
[Here are the two JSON Files]
SQL Query results JSON file:
[
{
"OnCallTeamName": "1 Park Ave Support Corporate Desktop Support Teams",
"ContactPrimaryLoginID": "chungn03",
"ContactSecondaryLoginID": "chungn03",
"FirstEscalationLoginID": "giakog01",
"SecondaryEscalationLoginID": null
},
{
"OnCallTeamName": "14 Wall Street Support Corporate Desktop Support Teams",
"ContactPrimaryLoginID": "gendua01",
"ContactSecondaryLoginID": "gendua01",
"FirstEscalationLoginID": "giakog01",
"SecondaryEscalationLoginID": null
},
"OnCallTeamName": "WUH Technology Managers",
"ContactPrimaryLoginID": "corond01",
"ContactSecondaryLoginID": "carela02",
"FirstEscalationLoginID": "corond01",
"SecondaryEscalationLoginID": null
},
{
"OnCallTeamName": "Xper",
"ContactPrimaryLoginID": "greenj23",
"ContactSecondaryLoginID": "greenj23",
"FirstEscalationLoginID": "daniet02",
"SecondaryEscalationLoginID": null
}
]
REST call JSON file:
[
{
"group_name": "Outage Management Team",
"group_onlinemembers": [
{
"user_username": "moraly01",
"user_firstname": "Yvette",
"user_lastname": "Morales",
"user_id": "58"
}
]
},
{
"group_name": "Ivanti HEAT",
"group_onlinemembers": [
{
"user_username": "vasquj01",
"user_firstname": "Jose",
"user_lastname": "Vasquez",
"user_id": "148"
},
{
"user_username": "moraly01",
"user_firstname": "Yvette",
"user_lastname": "Morales",
"user_id": "58"
}
]
},
{
"group_name": "Telecommunications",
"group_onlinemembers": [
{
"user_username": "moraly01",
"user_firstname": "Yvette",
"user_lastname": "Morales",
"user_id": "58"
},
{
"user_username": "fernad04",
"user_firstname": "David",
"user_lastname": "Fernandez",
"user_id": "150"
},
{
"user_username": "efg213",
"user_firstname": "Edwin",
"user_lastname": "Guerrero",
"user_id": "76"
},
{
"user_username": "reyesl07",
"user_firstname": "Louis",
"user_lastname": "Reyes",
"user_id": "75"
}
]
},
{
"group_name": "NYU Data Center Operations",
"group_onlinemembers": [
]
}
]
I need to know how to use powershell to loop through these JSON Files and only pull out the matching items

Parse data from JSON in ReactJS

I have data like this:
{
"movies": [
{
"abridged_cast": [
{
"characters": [
"Dominic Toretto"
],
"id": "162652472",
"name": "Vin Diesel"
},
{
"characters": [
"Brian O'Conner"
],
"id": "162654234",
"name": "Paul Walker"
},
{
"characters": [
"Louie Tran"
],
"id": "162684066",
"name": "Tony Jaa"
},
{
"characters": [
"Deckard Shaw"
],
"id": "162653720",
"name": "Jason Statham"
},
{
"characters": [
"Luke Hobbs"
],
"id": "770893686",
"name": "Dwayne \"The Rock\" Johnson"
}
],
"alternate_ids": {
"imdb": "2820852"
},
"critics_consensus": "",
"id": "771354922",
"links": {
"alternate": "http://www.rottentomatoes.com/m/furious_7/",
"cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922/cast.json",
"reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922/reviews.json",
"self": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922.json",
"similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/771354922/similar.json"
},
"mpaa_rating": "PG-13",
"posters": {
"detailed": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg",
"original": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg",
"profile": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg",
"thumbnail": "http://resizing.flixster.com/pVDoql2vCTzNNu0t6z0EUlE5G_c=/51x81/dkpu1ddg7pbsk.cloudfront.net/movie/11/18/14/11181482_ori.jpg"
},
"ratings": {
"audience_rating": "Upright",
"audience_score": 88,
"critics_rating": "Certified Fresh",
"critics_score": 82
},
"release_dates": {
"theater": "2015-04-03"
},
"runtime": 140,
"synopsis": "Continuing the global exploits in the unstoppable franchise built on speed, Vin Diesel, Paul Walker and Dwayne Johnson lead the returning cast of Fast & Furious 7. James Wan directs this chapter of the hugely successful series that also welcomes back favorites Michelle Rodriguez, Jordana Brewster, Tyrese Gibson, Chris \"Ludacris\" Bridges, Elsa Pataky and Lucas Black. They are joined by international action stars new to the franchise including Jason Statham, Djimon Hounsou, Tony Jaa, Ronda Rousey and Kurt Russell.",
"title": "Furious 7",
"year": 2015
}
]
}
I need to parse all data from all fields from this JSON file. Is there a way to do it in React JS? Could you please suggest me any way to parse data from a structured JSON file like this?
React lives in JavaScript. So parsing a JSON string is done with:
var myObject = JSON.parse(myjsonstring);
How to fetch a file from somewhere with AJAX is a different question.
You could use fetch() for this. See for example
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API or
https://davidwalsh.name/fetch or
https://blog.gospodarets.com/fetch_in_action

JSON object extract from big query database using app script

I have a big query table that has a JSON object as one of the field in the table. How can I extract the data from the JSON object using app script. The object itself is nested. It looks like this
{
"uid": "124551",
"subjects": [
{
"tid": 37,
"title": "Algebra",
"html_id": "algebra",
"selected": true
},
{
"tid": 214853,
"title": "Trigonometry",
"html_id": "trigonometry",
"selected": true
},
{
"tid": 38,
"title": "Geometry",
"html_id": "geometry",
"selected": true
}
],
"cellphone": "09178854579",
"educations": [
{
"index": 0,
"schoolname": "University of the Philippines - Los BaƱos",
"degree": "BS Mathematics",
"major": "Mathematics",
"eduFrom": "2009-05-31T16:00:00.000Z",
"eduTo": "2013-04-26T16:00:00.000Z",
"eduFromTs": 1243785600,
"eduToTs": 1366992000
}
],
"info": {
"os": "Windows",
"internet": "ADSL",
"browser": "Chrome",
"network": "Wireless",
"speed": "",
"timezone": "Asia/Hong_Kong"
}
}
I want to extract all school names from education field. Any ideas?
Working with JSON objects is similar to working with XML, except that parsing or encoding a JSON object is much easier.
Once this string is retrieved, simply call JSON.parse() on the string to get a native object representation.
var data = JSON.parse(json);
Browser.msgBox(data.info.os);
Other sample code is at https://developers.google.com/apps-script/advanced/bigquery#run_query