My very first question after using this site for my own learning! Still a beginner so go easy on me :)
I am trying to format JSON data within MSSQL Server. I have a static JSON file which I can get to display via OPENROWSET, and populate a variable. This JSON file has a "header" and then one or more "child" rows, basically an order header and order detail lines. I can successfully separately display the header's columns as a table in a result-set. I'd like to do the same with just the detail lines - the aim being to then store the header in a table and it's details in a table within SQL server - this part I'll have no issue with.
Here is some mock-up JSON data that I'm working with. This is the exact format I need to use, so I don't have any room to manoeuvre with it but I've populated it with test data:
{
"InputParameters" : {
"P_IN_ORDER_SOURCE" : "The_Web",
"P_IN_ORIG_SYS_DOCUMENT_REF" : "Order666",
"P_IN_SOLD_TO_CUST_NUMBER" : "JOEB11",
"P_IN_CUST_ORDER_NUMBER" : "JoeB5556667",
"P_IN_REQUEST_DATE" : "2021-01-20 08:10:06",
"P_IN_ORDER_ENTRY_DATE" : "2021-01-20 08:10:06",
"P_IN_SHIPTO_NAME" : "The Testing Co.",
"P_IN_SHIPTO_ADDR" : "82 Annweir Crescent",
"P_IN_SHIPTO_ADDR_2" : null,
"P_IN_SHIPTO_CITY" : "Atlantis",
"P_IN_SHIPTO_STATE" : "WSX",
"P_IN_SHIPTO_ZIP" : "AT55 666",
"P_IN_SHIPTO_COUNTRY" : "GB",
"P_IN_OPERATION_CODE" : "CREATE",
"P_IN_BOOKED_FLAG" : "N",
"P_IN_OU_NAME" : "ATL UK OU",
"P_IN_SPECIAL_INSTRUCTIONS" : null,
"P_IN_QUOTE_NUMBER" : null,
"P_IN_PRICELIST_ID" : "8",
"P_IN_EMAIL" : "testemail#testemail.com",
"P_IN_SHIPTO_COUNTY" : null,
"P_IN_SHIPPING_METHOD" : "Pre 930",
"P_IN_SHIPPING_INSTRUCTIONS" : null,
"P_IN_ATTENTION_TO" : "Joe Bloggs",
"P_IN_FREIGHT_CARRIER_CODE" : null,
"P_IN_IS_RETURN" : null,
"P_IN_SALES_REP" : null,
"P_IN_LINE_DATA" : [ {
"P_IN_LINE_DATA_ITEM" : [ {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "1",
"CUSTOMER_LINE_NUMBER" : "1",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU7776",
"USER_ITEM_DESCRIPTION" : "SKU7776",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "16.95",
"UNIT_SELLING_PRICE" : "16.95",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "2",
"CUSTOMER_LINE_NUMBER" : "2",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU12345",
"USER_ITEM_DESCRIPTION" : "SKU12345",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "11.89",
"UNIT_SELLING_PRICE" : "11.89",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "3",
"CUSTOMER_LINE_NUMBER" : "3",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU9999",
"USER_ITEM_DESCRIPTION" : "SKU9999",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "8",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "46.42",
"UNIT_SELLING_PRICE" : "46.42",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
} ]
} ]
}
}
I've been trying to learn how to use this JSON with SQL server pretty much starting from today. I've explored the OPENJSON() function which like I've said, I can define the separate columns and path with the header information - but as soon as I try to do similar and path to the detail objects, I just get NULLs back in each column.
Any suggestions at all? Apologies if I've missed any key information out here! Many thanks!
Something like this:
declare #json nvarchar(max) = '
{
"InputParameters" : {
"P_IN_ORDER_SOURCE" : "The_Web",
"P_IN_ORIG_SYS_DOCUMENT_REF" : "Order666",
"P_IN_SOLD_TO_CUST_NUMBER" : "JOEB11",
"P_IN_CUST_ORDER_NUMBER" : "JoeB5556667",
"P_IN_REQUEST_DATE" : "2021-01-20 08:10:06",
"P_IN_ORDER_ENTRY_DATE" : "2021-01-20 08:10:06",
"P_IN_SHIPTO_NAME" : "The Testing Co.",
"P_IN_SHIPTO_ADDR" : "82 Annweir Crescent",
"P_IN_SHIPTO_ADDR_2" : null,
"P_IN_SHIPTO_CITY" : "Atlantis",
"P_IN_SHIPTO_STATE" : "WSX",
"P_IN_SHIPTO_ZIP" : "AT55 666",
"P_IN_SHIPTO_COUNTRY" : "GB",
"P_IN_OPERATION_CODE" : "CREATE",
"P_IN_BOOKED_FLAG" : "N",
"P_IN_OU_NAME" : "ATL UK OU",
"P_IN_SPECIAL_INSTRUCTIONS" : null,
"P_IN_QUOTE_NUMBER" : null,
"P_IN_PRICELIST_ID" : "8",
"P_IN_EMAIL" : "testemail#testemail.com",
"P_IN_SHIPTO_COUNTY" : null,
"P_IN_SHIPPING_METHOD" : "Pre 930",
"P_IN_SHIPPING_INSTRUCTIONS" : null,
"P_IN_ATTENTION_TO" : "Joe Bloggs",
"P_IN_FREIGHT_CARRIER_CODE" : null,
"P_IN_IS_RETURN" : null,
"P_IN_SALES_REP" : null,
"P_IN_LINE_DATA" : [ {
"P_IN_LINE_DATA_ITEM" : [ {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "1",
"CUSTOMER_LINE_NUMBER" : "1",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU7776",
"USER_ITEM_DESCRIPTION" : "SKU7776",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "16.95",
"UNIT_SELLING_PRICE" : "16.95",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "2",
"CUSTOMER_LINE_NUMBER" : "2",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU12345",
"USER_ITEM_DESCRIPTION" : "SKU12345",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "6",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "11.89",
"UNIT_SELLING_PRICE" : "11.89",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
}, {
"ORIG_SYS_DOCUMENT_REF" : "Order666",
"ORIG_SYS_LINE_REF" : "3",
"CUSTOMER_LINE_NUMBER" : "3",
"ITEM_TYPE_CODE" : "STANDARD",
"ITEM_DESCRIPTION" : "SKU9999",
"USER_ITEM_DESCRIPTION" : "SKU9999",
"TOP_MODEL_LINE_REF" : null,
"LINK_TO_LINE_REF" : null,
"COMPONENT_CODE" : null,
"ORDERED_QUANTITY" : "8",
"ORDER_QUANTITY_UOM" : null,
"UNIT_LIST_PRICE" : "46.42",
"UNIT_SELLING_PRICE" : "46.42",
"CALCULATE_PRICE_FLAG" : "N",
"OPERATION_CODE" : "INSERT"
} ]
} ]
}
}
'
select *
from openjson(#json,'$.InputParameters.P_IN_LINE_DATA[0].P_IN_LINE_DATA_ITEM')
with
(
ORIG_SYS_DOCUMENT_REF varchar(200),
ORIG_SYS_LINE_REF int,
CUSTOMER_LINE_NUMBER int,
ITEM_TYPE_CODE varchar(200),
-- . . .
OPERATION_CODE varchar(200)
)
Related
i want to send json body to my middleware. at first, it works well (note that the "tema" & "emailGroup" wasn't an array). but after some changes on my backend, i have to send this type of json
[{
"ID": "",
"Name": "Artikel BU CE - Visit HoB Topic",
"ChannelType": 0,
"PublishDate": "2018-09-21T01:00:00Z",
"Headline": null,
"Content": null,
"EmailSubject": null,
"EmailUrl": null,
"Mention": null,
"PostLink": null,
"ChannelActivityMobileId": null,
"HashTag": null,
"Tema": [
{
"Value": 6
}
],
"EmailGroup": [
{
"ID": "2c53ea1f-6ebe-e811-a977-000d3aa00fc2",
"Name": "TV Broadcast",
"List_EmailListModels": null
}
],
"ApprovalStatus": 0,
"ApprovalNote": null,
"EmployeeId": null,
"EmployeeLevel": 0
}]
here's my code
let parameters = [["ID" : "", SerializationKeys.channelMobileId : channel.mobileId, SerializationKeys.name : activity.activityName, "ApprovalStatus" : channel.channelStatus, SerializationKeys.channelType : channel.channelType, SerializationKeys.publish_date : channel.publishDate, SerializationKeys.content : channel.content, SerializationKeys.emailSubject : channel.emailSubject, SerializationKeys.emailURL : channel.emailURL, SerializationKeys.hashtag : channel.hastag, SerializationKeys.mention : channel.mention, SerializationKeys.note : channel.note, SerializationKeys.postLink : channel.postLink, SerializationKeys.tema : [tema]] as [[String : Any]]
where tema is
var tema = [String : Int]()
i got an error that says
[Any] is not convertible to '[[String : Any]]'; did you mean to use as! to force downcast?
and after i change "as" to "as!", it says
Excpected ";" separator
where the semicolon should put near "SerializationKeys.tema : [tema]". Please kindly help me. Thanks
EDIT
I think you mistakenly added ] near
, SerializationKeys.postLink : channel.postLink],
You may want this
let parameters:[[String:Any]] = [["ID" : "",
SerializationKeys.channelMobileId : channel.mobileId,
SerializationKeys.name : activity.activityName,
"ApprovalStatus" : channel.channelStatus,
SerializationKeys.channelType : channel.channelType,
SerializationKeys.publish_date : channel.publishDate,
SerializationKeys.content : channel.content,
SerializationKeys.emailSubject : channel.emailSubject,
SerializationKeys.emailURL : channel.emailURL,
SerializationKeys.hashtag : channel.hastag,
SerializationKeys.mention : channel.mention,
SerializationKeys.note : channel.note,
SerializationKeys.postLink : channel.postLink,
SerializationKeys.tema : [tema]
]]
Note: I heighly recommend using Codable with struct models for your case
Below is the JSON file I am trying to read nodes from. I am trying to perform contains operation. I do not want to use equals option in my json path like this - $.[?(#.name=="College Graduate - Ford")]
Could you please help me in meeting my requirement?
[
{
"incentiveId" : 123,
"autoApplied" : "false",
"name" : "College Graduate - Ford",
"amount" : 750,
"expirationDate" : "2018-12-31",
"minimumTerm" : null,
"maximumTerm" : null,
"groupAffiliation" : "College/Student",
"previousOwnership" : null
},
{
"incentiveId" : 456,
"autoApplied" : "false",
"name" : "Lease Loyalty - Ford",
"amount" : 500,
"expirationDate" : "2018-07-09",
"groupAffiliation" : null,
"previousOwnership" : "Lease Loyalty"
},
{
"incentiveId" : 789,
"autoApplied" : "false",
"name" : "Customer Cash - Ford",
"amount" : 1000,
"expirationDate" : "2018-06-04",
"groupAffiliation" : null,
"previousOwnership" : null
},
{
"incentiveId" : 222,
"autoApplied" : "false",
"name" : "Military - Ford",
"amount" : 1000,
"expirationDate" : "2018-12-31",
"groupAffiliation" : "Military",
"previousOwnership" : null
}
]
$.[?(/College/.test(#.name))]
Here I am using the test the RegExp test( ) method (which JSONPath evals behind the scenes) . This will perform the contains operation.
you can use filter to get array from array which elements satisfy the condition.
colleagues = json.filter(node => node.name.match("College"))
result:
[
{
"incentiveId": 123,
"autoApplied": "false",
"name": "College Graduate - Ford",
"amount": 750,
"expirationDate": "2018-12-31",
"minimumTerm": null,
"maximumTerm": null,
"groupAffiliation": "College/Student",
"previousOwnership": null
}
]
Hello guys I'm new to angularjs.I'm trying to apply filter on the following thing.But what happening is it is not iterating all list values with ng-repeat followed by some filters.It only iterating jobseekerId 1,2 only but not 3.Where as if I'm removing the filter means it iterating all list values(1,2,3).
What is the problem with my code .Please give some suggestion.
This is my data
[ {
"jobSeekerId" : 1,
"firstName" : "vijay",
"middleName" : null,
"lastName" : "sury",
"jobSeekerProfTitle" : "Having 2.2yerars of experience in the field java development with spring and hibernate",
"profileUpdateDate" : "05 May 2014",
"currentState" : "ap",
"profilePath" : "E:/opt/jsimages/recru-profile.jpg",
"currentCity" : "hyd",
"salary" : "20000",
"salaryType" : "per month",
"jobSeekerSkillVo" : [ {
"skill" : "js"
}, {
"skill" : "java"
}, {
"skill" : "uuu"
} ],
"academicInfoVo" : [ {
"academicInfoId" : 1,
"courseName" : "btech",
"universityName" : "intuk",
"academicEndYear" : 2014,
"gpa" : 10.0
} ],
"city" : [ "hyd", "ts" ],
"totalExperience" : "48",
"viewCount" : "2",
"downloadCount" : "0",
"viewStatus" : "alive"
}, {
"jobSeekerId" : 2,
"firstName" : "raj",
"middleName" : null,
"lastName" : "sury",
"jobSeekerProfTitle" : null,
"profileUpdateDate" : null,
"currentState" : null,
"profilePath" : null,
"currentCity" : null,
"salary" : null,
"salaryType" : null,
"jobSeekerSkillVo" : [ {
"skill" : "uuu"
}, {
"skill" : "java"
} ],
"academicInfoVo" : [ {
"academicInfoId" : 2,
"courseName" : "btech",
"universityName" : "intuk",
"academicEndYear" : 2014,
"gpa" : 8.0
} ],
"city" : [ ],
"totalExperience" : "14",
"viewCount" : "1",
"downloadCount" : "0",
"viewStatus" : "alive"
}, {
"jobSeekerId" : 3,
"firstName" : "vj",
"middleName" : null,
"lastName" : "gg",
"jobSeekerProfTitle" : null,
"profileUpdateDate" : null,
"currentState" : null,
"profilePath" : null,
"currentCity" : null,
"salary" : "20000",
"salaryType" : "per month",
"jobSeekerSkillVo" : [ ],
"academicInfoVo" : [ ],
"city" : [ ],
"totalExperience" : "0",
"viewCount" : "1",
"downloadCount" : "0",
"viewStatus" : "alive"
} ]
======================
This is my filter
<div class="wrapper" ng-app="candidateListApp" ng-controller="candidateListController">
<span ng-cloak ng-repeat="jsList in jobSeekerList|filter:{jobSeekerSkillVo:
{skill:drpSkill},})}}">
<a ng-href="#">
{{jsList.firstName}} {{jsList.lastName}}
</a>
</span>
<span ng-if="jsList.jobSeekerSkillVo.length != 0">
<span ng-repeat="skills in jsList.jobSeekerSkillVo">
{{skills.skill}}
{{$last?'':','}}
</span>
</span>
</div>
<script>
var app=angular.module("candidateListApp",[]);
app.controller("candidateListController",function($scope,$http){
$scope.jobSeekerList=${jobSeekerJsonArray};
});
</script>
This is my model (with jsp code)
<select id="skills" name="skills" class="form-control2" ng-model="drpSkill">
<option value="" disabled="disabled" selected="selected">Skills</option>
<c:forEach items="${skillVo}" var="skills">
<option value="${skills.skill}">${skills.skill}</option>
</c:forEach>
</select>
The content of my JSON file is:
This is my retails.json file.actually retails is my database.i converted this json file from sql database.so in this json file, I want to delete a person information with a single ID.How can i do? i am using node.js and express.
{
"categories" : [
{
"dept_id" : "123",
"category_name" : "database",
"category_discription" : "list of database",
"current time" : "2016-07-21 06:27:17",
"id" : "1"
},
{
"dept_id" : "1234",
"category_name" : "debugging",
"category_discription" : "program debugger",
"current time" : "2016-07-21 06:32:24",
"id": "2"
},
{
"dept_id" : "12345",
"category_name" : "system analyzer",
"category_discription" : null,
"current time" : "2016-07-21 06:33:23",
"id" : "3"
}
],
"departments" : [
{
"name" : "manpreet singh",
"address_info" : "qadian",
"current time" : null,
"id" : "1"
},
{
"name" : "tushal gupta",
"address_info" : "amritsar",
"current time" : "2016-07-21 06:10:14",
"id" : "2"
},
{
"name" : "haroop singh",
"address_info" : "amritsar",
"current time" : "2016-07-21 06:11:12",
"id" : "3"
}
],
"digital_marketing" : [
{
"dept_id" : "123",
"phone" : "99889988",
"mobile" : null,
"email" : "thbs#gmail.com",
"web" : null,
"facebook" : null,
"twitter" : null,
"linkedin" : null,
"current time" : "2016-07-21 06:10:16",
"id" : "1"
},
{
"dept_id" : "1234",
"phone" : "998899888",
"mobile" : null,
"email" : null,
"web" : null,
"facebook" : "gtudgal#fb.com",
"twitter" : "tushalgupta",
"linkedin" : null,
"current time" : "2016-07-21 06:30:19",
"id" : "2"
},
{
"dept_id" : "12345",
"phone" : "99889877",
"mobile" : null,
"email" : "fhdts#mail.com",
"web" : null,
"facebook":"sdfh33#fb.com",
"twitter" : null,
"linkedin" : null,
"current time" : "2016-07-21 06:30:13",
"id" : "3"
}
]
}
I am using this to delete a ID, but does not work:
var id = 2;
app.get('/deleteUser', function (req, res) {
// First read existing users.
fs.readFile( __dirname + "/" + "retails.json", 'utf8', function (err, data) {
data = JSON.parse( data );
delete data["categories" + 2];
console.log( data );
res.end( JSON.stringify(data));
});
});
for (var prop in jsonObj.categories){
delete jsonObj.categories[prop].id;
}
similar for digital_marketing etc. jsonObj is the data in your case..
I have to convert a JSON file in a CSV file or even better, extract the JSON file content in to an Oracle table.
I gave a look into the other topics and the only one that I found is related to the conversion in HTML table.
The files are really unstructured, here an example:
{
"message-version" : "1.2",
"orcid-profile" : {
"orcid" : null,
"orcid-id" : null,
"orcid-identifier" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"orcid-deprecated" : null,
"orcid-preferences" : {
"locale" : "EN"
},
"orcid-history" : {
"creation-method" : "MEMBER_REFERRED",
"completion-date" : null,
"submission-date" : {
"value" : 1417193651961
},
"last-modified-date" : {
"value" : 1418131422749
},
"claimed" : {
"value" : true
},
"source" : null,
"deactivation-date" : null,
"verified-email" : {
"value" : true
},
"verified-primary-email" : {
"value" : true
},
"visibility" : null
},
"orcid-bio" : {
"personal-details" : {
"given-names" : {
"value" : "Marcin"
},
"family-name" : {
"value" : "Serocki"
},
"credit-name" : null,
"other-names" : null
},
"biography" : null,
"researcher-urls" : null,
"contact-details" : null,
"keywords" : null,
"external-identifiers" : null,
"delegation" : null,
"applications" : null,
"scope" : null
},
"orcid-activities" : {
"affiliations" : {
"affiliation" : [ {
"type" : "EDUCATION",
"department-name" : "Department of Pharmaceutical Technology and Biochemistry",
"role-title" : "PhD",
"start-date" : {
"year" : {
"value" : "2010"
},
"month" : {
"value" : "09"
},
"day" : {
"value" : "01"
}
},
"end-date" : null,
"organization" : {
"name" : "Gdansk University of Technology",
"address" : {
"city" : "Gdansk",
"region" : null,
"country" : "PL"
},
"disambiguated-organization" : null
},
"source" : {
"source-orcid" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"source-client-id" : null,
"source-name" : {
"value" : "Marcin Serocki"
},
"source-date" : {
"value" : 1418131402196
}
},
"created-date" : {
"value" : 1418131402196
},
"last-modified-date" : {
"value" : 1418131402196
},
"visibility" : "PUBLIC",
"put-code" : "611828"
}, {
"type" : "EDUCATION",
"department-name" : "Department of Pharmaceutical Technology and Biochemistry",
"role-title" : "MSc",
"start-date" : {
"year" : {
"value" : "2005"
},
"month" : {
"value" : "09"
},
"day" : {
"value" : "01"
}
},
"end-date" : {
"year" : {
"value" : "2010"
},
"month" : {
"value" : "07"
},
"day" : {
"value" : "01"
}
},
"organization" : {
"name" : "Gdansk University of Technology",
"address" : {
"city" : "Gdansk",
"region" : null,
"country" : "PL"
},
"disambiguated-organization" : null
},
"source" : {
"source-orcid" : {
"value" : null,
"uri" : "http://orcid.org/0000-0002-3285-9536",
"path" : "0000-0002-3285-9536",
"host" : "orcid.org"
},
"source-client-id" : null,
"source-name" : {
"value" : "Marcin Serocki"
},
"source-date" : {
"value" : 1418131329327
}
},
"created-date" : {
"value" : 1418131329327
},
"last-modified-date" : {
"value" : 1418131329327
},
"visibility" : "PUBLIC",
"put-code" : "611826"
} ]
},
"orcid-works" : null,
"funding-list" : null
},
"orcid-internal" : null,
"type" : "USER",
"group-type" : null,
"client-type" : null
},
"orcid-search-results" : null,
"error-desc" : null
}
I have no idea how to proceed?
All code is available at github.
The code is updated according to the changed did in csv in 2013 since then CSVs now have headers! The key name for the field is now the first row of the CSV.
https://github.com/vinay20045/json-to-csv
OR
You can use api
https://json-csv.com/api to convert.
Just register there get api accessibility privileges and use thier code. That's it