We have a json object provided by vendor with naming conventions suitable only for vendor system. We would like to rename all the json attributes names matching our company's naming standards. I showed a very small XML file but in reality we have a huge XML with many elements. The transformed json object will eventually be persisted in our datastore.Any inputs in handling efficient way?
Json Example Provided by Vendor
{
"Events": [
{
"TestOn": {
"TestCode": "32",
"TestSubCode": "11",
"TestStatus": "4",
},
"Timers": {
"TestOpCode": "10",
"TestMode": "4",
"TestItemTime": "4",
}
}
],
"Ticket": [
{
"Discount": {
"OpCode": "3",
"CluCode": "00001530020003",
"DeptNo": "11",
},
"Promotion": {
"OpCode": "96",
"SubCode": "26",
"F-Promotion": "1",
}
}
]
}
Transformed Json Object
{
"PxEvents": [
{
"PxTestOn": {
"PxTestCode": "32",
"PxTestSubCode": "11",
"PxTestStatus": "4",
},
"PxTimers": {
"PxTestOpCode": "10",
"PxTestMode": "4",
"PxTestItemTime": "4",
}
}
],
"PxTicket": [
{
"PxDiscount": {
"PxOpCode": "3",
"PxCluCode": "00001530020003",
"DeptNo": "11",
},
"PxPromotion": {
"PxOpCode": "96",
"PxSubCode": "26",
"PxPromotion": "1",
}
}
]
}
Related
I am using for each to iterate and get values of a json object in my solution.
Let me give example of the json.
[
{
"#Seq": "1",
"#text": "123456789"
},
{
"#Seq": "2",
"#text": "abc"
},
{
"#Seq": "3",
"#text": "xyz"
}
]
It works well for multiple sequences until we have only one seq in the json.
example:
{{
"#Seq": "1",
"#text": "123456789"
}}
Coming to Code:
foreach (var installinst in atpmessage["AUTOPILOTMessage"]["AUTOPILOTBody"]["OrderHeader"]["InstallationInstructions"]["InstallationInstruction"])
{
responseISGEMEA.data.install_instruct =installinst["#text"].ToString();
}
The installinst in the loop gives me just the Seq. Not the text.
{"#Seq": "1"}
how do I make it work with single and multiple sequences.
I'm trying to find the shortest path along with relations on the nodes on the path, for which below query is used.
MATCH p = shortestPath((p1:Person { name: 'Kevin Bacon' })-[*..15]-
(p2:Person { name: 'Meg Ryan' }))
UNWIND nodes(p) as n
MATCH (n)-[*]->(q)
RETURN n, q
However i want to return the result as json object with data format as below in c#. I understand we have to use apoc. However can't really understand how to proceed.
{
"results": [
{
"data": [
{
"graph": {
"nodes": [
{
"id": "1",
"labels": ["James"],
"properties": {
"ShortName": "jammy",
"Type": "Person",
"Age": 34
}
},
{
"id": "2",
"labels": ["Brad"],
"properties": {
"name": "Brad",
"PlaceOfBirth": "California",
"Type": "Person",
"description": "Nice actor",
}
},
{
"id": "3",
"labels": ["Titanic"],
"properties": {
"movieName": "Titanic",
"Type": "Movie",
"description": "Tragedy",
}
}
],
"relationships": [
{
"id": "4",
"type": "ACTED_IN",
"startNode": "1",
"endNode": "3",
"properties": {
"from": 1470002400000
}
}
]
}
}
]
}
],
"errors": []
}
You can collect the nodes and relationships separately and add it on the result.
MATCH p = shortestPath((p1:Person { name: 'Kevin Bacon' })-[*..15]-(p2:Person { name: 'Meg Ryan' }))
UNWIND nodes(p) as n
MATCH (n)-[r]->(q)
WITH collect(distinct n) + collect(distinct q) as node_list, collect(distinct r) as rel_list
RETURN {results: {data: {graph: {nodes: node_list, relationships: rel_list}}, error: []}} as output
I wanted the first level incoming and outgoing relations of all the nodes on the path. Slighty modified the answer for anyone looking for something similar in future. Thanks Jose.
MATCH p = shortestpath((p1:Person { name: 'Kevin Bacon' })-[*..30]-
(p2:Person { name: 'Meg Ryan' }))
UNWIND nodes(p) as n
MATCH (n)<-[r*1]->(q)
WITH collect(distinct n) + collect(distinct q) as node_list,
collect(distinct r) as rel_list
RETURN {results: {data: {graph: {nodes: node_list, relationships:
rel_list}}, error: []}} as output
I have a JSON object like in below.
{
"?xml": {
"#version": "1.0",
"#encoding": "UTF-8"
},
"?xml-stylesheet": "type=\"text/xsl\" href=\"isokur.xsl\"",
"Tarih_Date": {
"#Tarih": "14.10.2022",
"#Date": "10/14/2022",
"#Bulten_No": "2022/196",
"Currency": [
{
"#CrossOrder": "0",
"#Kod": "USD",
"#CurrencyCode": "USD",
"Unit": "1",
"Isim": "ABD DOLARI",
"CurrencyName": "US DOLLAR",
"ForexBuying": "18.5596",
"ForexSelling": "18.5930",
"BanknoteBuying": "18.5466",
"BanknoteSelling": "18.6209",
"CrossRateUSD": null,
"CrossRateOther": null
},
{
"#CrossOrder": "1",
"#Kod": "AUD",
"#CurrencyCode": "AUD",
"Unit": "1",
"Isim": "AVUSTRALYA DOLARI",
"CurrencyName": "AUSTRALIAN DOLLAR",
"ForexBuying": "11.6732",
"ForexSelling": "11.7493",
"BanknoteBuying": "11.6195",
"BanknoteSelling": "11.8198",
"CrossRateUSD": "1.5862",
"CrossRateOther": null
}
]
}
}
And I want to grab only the this part from it. Where #CurrencyCode == "USD"
[
{
"#CrossOrder": "0",
"#Kod": "USD",
"#CurrencyCode": "USD",
"Unit": "1",
"Isim": "ABD DOLARI",
"CurrencyName": "US DOLLAR",
"ForexBuying": "18.5596",
"ForexSelling": "18.5930",
"BanknoteBuying": "18.5466",
"BanknoteSelling": "18.6209",
"CrossRateUSD": null,
"CrossRateOther": null
}
]
For this purpose, I created the code below according to this answer.
string result = JObject.Parse(json)["Tarih_Date"].SelectToken("$.Currency[?(#.#CurrencyCode=='USD')]")["Currency"].ToString();
But I couldn't make it word and I get the exception below. As I understand, my query doesn't find it's way(?) But couldn't understand where it is.
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Anything helps!
You get the exception because there is no property called Currency in the output of SelectToken. You will get the desired output by switching to:
var result = JObject.Parse(json)["Tarih_Date"].SelectToken("$.Currency[?(#.#CurrencyCode == 'USD')]");
I'm sure that somewhere there is a simple answer to my question, but I couldn't fin it. The problem I faced is following, I have code structure:
class Field
{
List<Block> Blocks {get;set;}
public static Field CreateField()
{
var A = new Block {Connector = new Connector()}
var B = new Block {Connector = new Connector()}
A.Connector.ConnectedTo.Add(B);
B.Connector.ConnectedTo.Add(A);
var field = new Field();
field.Blocks = new List {A, B};
return field;
}
}
class Block
{
Connector Connector {get;set;}
}
class Connector
{
List<Block> ConnectedTo {get;set;}
}
Than serialization command using json.net:
JsonConvert.SerializeObject(Field.CreateField(), Formatting.Indented, new JsonSerializerSettings {PreserveReferencesHandling = PreserveReferencesHandling.Objects});
and output result is as expected:
{
"$id": "1",
"Blocks": [
{
"$id": "2",
"Connector": {
"$id": "3",
"ConnectedTo": [
{
"$id": "4",
"Connector":{
"$id": "5",
"ConnectedTo": [
{
"$ref": "2"
}
]
}
}
]
}
}
]
}
Is there any way to have serialization result as following:
{
"$id": "1",
"Blocks": [
{
"$id": "2",
"Connector": {
"$id": "3",
"ConnectedTo": [
{
"$ref": "4"
}
]
}
},
{
"$id": "4",
"Connector":{
"$id": "5",
"ConnectedTo": [
{
"$ref": "2"
}
]
}
}
]
}
Thanks.
No. In your second example, $ref : 4 points to an $id that occurs later in the JSON. Json.Net will not be able to deserialize this-- when it comes upon a $ref it expects to be able to resolve it to an $id that it has already seen.
I would recommend against requiring users modify JSON directly anyway unless they are technically savvy. As I've seen from answering many JSON-related questions on SO, it is very easy to get the format messed up if you edit it manually-- missing quotes or commas, mismatched braces, invalid syntax, you name it. What will you do when someone screws it up? I think a better option is to make a tool that allows manipulating your structure abstractly. The tool could deserialize the original JSON, allow certain changes and then reserialize it into the new structure. Users would not need to know anything about the underlying JSON format that way.
I've tried different solutions but I'm not reaching anywhere.
I'm in a middle of a loop and sometimes I need to add data to an existing property (details in this case).
So, in the beginning I create the following JObject without any problem:
var json = JsonConvert.SerializeObject(
new {
details = new[]{
new{product_name = detail["product_name"].ToString(),
quantity = detail["quantity"].ToString(),
product_options = detail["product_options"].ToString()},
}
}
);
// _elements is an dictionary<int, JObject>
_elements.Add(id, JObject.Parse(json));
// output
{
"details": [
{
"product_name": "Oranges",
"quantity": "2",
"product_options": [],
}
]
}
But, for some reason, I need to add more products to the list of details, so I would like my output to be:
{
"details": [
{
"product_name": "Oranges",
"quantity": "2",
"product_options": [],
},
{
"product_name": "Coca Cola",
"quantity": "5",
"product_options": [],
}
]
}
I've tried so far without any success:
dic.Value.Property("details").Add(json);
dic.Value.SelectToken("details").Add(json);
Solved.
dic.Value["details"].Last.AddAfterSelf(JObject.Parse(json));