jsonpath- Extracting json data using jsonpath - json

My json data is as follows:
"query": {
"count": 1,
"url": [
{
"execution-start-time": "1"
},
{
"execution-time": "745"
}
],
"service-time": "1481"
},
results": {
"div": {
"class": "makers",
"ul": {
"li": [
{
"a": {
"href": "nokia_x2_dual_sim-6383.php",
"img": {
"src": "Nokia-X2-Dual-SIM.jpg"
},
"strong": {
"br": null,
"content": "Nokia\nX2 Dual SIM"
}
}
},
{
"a": {
"href": "nokia_xl-6148.php",
"img": {
"src": "nokia-xl.jpg",
},
"strong": {
"br": null,
"content": "Nokia\nXL"
}
}
}
]
Now I want to extract the "content" part from this json data using jsonpath.I am using jsonpath-0.8.0.js for this purpose.
I have tried to parse the json data in the following way:
function ParseData(data) {
var result = jsonPath(data, "$.query.results[*].ul.li[*].strong.content");
$('#carousel').empty();
var html = "";
for (i = 0; i < result.length; i++) {
// do something
}
and I was expecting to get Nokia\nX2 Dual SIM and Nokia\nXL as output but this code of mine does not return anything.I cannot understand the problem.
How do I extract the content data from this json? please help.

Friends, I finally got the answer to my problem.I used the following code to extract content from the above given json data.
var result = jsonPath(data, "$.query.results[*].ul.li[*].a.strong.content");
Now I am getting the output data as expected.

Related

convert JavaScript nested array for priming tree format

i have following JAVASCRIPT OBJECT and i need to convert it to primeng tree format , please help
INPUT
{
"com": {
"ups": {
"demo": {
"a": 9
}
}
}
}
OUTPUT expected
[
{
"label": "COM",
"data": "COM",
"children": [{
"label": "ABC",
"data": "abc",
"children": [ "label": "x" data": "x" ,children:[]]
}]
}]
Working Example
validate(a) {
let newArr = [];
for (const key in a) {
if (key) {
newArr.push({data: key, label: key, childern: this.validate(a[key])});
}
}
return newArr;
}
const a = {
"com": {
"ups": {
"demo": {
"a": 9
}
}
}
};
console.log(this.validate(a));

Dwg comparison through design automation

I would like to know if it is possible, and if yes, how can I achieve dwg comparison through the design automation? I there a way to create a comparison activity accepting 2 dwg in input, and post a boolean as output?
Yes you can create an activity accepting two drawings.
Following activity JSON uses an in-built compare and output the result diff drawing.
Sample Activity Json
{
"HostApplication": "",
"RequiredEngineVersion": "23.0",
"Parameters": {
"InputParameters": [
{
"Name": "HostDwg",
"LocalFileName": "$(HostDwg)"
},
{
"Name": "ToCompareWith",
"LocalFileName": "ToCompareWith.dwg"
}
],
"OutputParameters": [
{
"Name": "Result",
"LocalFileName": "output.dwg"
}
]
},
"Instruction": {
"CommandLineParameters": null,
"Script": "COMPAREINPLACE\nON\n-COMPARE\n\nToCompareWith.dwg\n_SAVEAS\n\noutput.dwg\n"
},
"Id": "FPDCompare"
}
Workitem Json
{
"Arguments": {
"InputArguments": [
{
"Resource": "https://madhukar-fda.s3.us-west-2.amazonaws.com/Kitchens1.dwg",
"Name": "HostDwg"
},
{
"Resource": "https://madhukar-fda.s3.us-west-2.amazonaws.com/Kitchens2.dwg",
"Name": "ToCompareWith"
}
],
"OutputArguments": [
{
"Name": "Result",
"HttpVerb": "POST"
}
]
},
"ActivityId": "FPDCompare"
}
If you are using your own custom compare logic, you can output the
results to txt file or json file.
Activity Json
{
"HostApplication": "",
"RequiredEngineVersion": "23.0",
"Parameters": {
"InputParameters": [
{
"Name": "HostDwg",
"LocalFileName": "$(HostDwg)"
},
{
"Name": "ToCompareWith",
"LocalFileName": "ToCompareWith.dwg"
}
],
"OutputParameters": [
{
"Name": "Result",
"LocalFileName": "output.txt"
}
]
},
"Instruction": {
"CommandLineParameters": null,
"Script": "ISDWGSIMILAR\nToCompareWith.dwg\n"
},
"AppPackages":["Compare"],
"Version": 1,
"Id": "Compare"
}
Workitem Json
{
"Arguments": {
"InputArguments": [
{
"Resource":"https://madhukar-fda.s3.us-west-2.amazonaws.com/Kitchens.dwg",
"Name": "HostDwg"
},
{
"Resource":"https://madhukar-fda.s3.us-west-2.amazonaws.com/Kitchens1.dwg",
"Name": "ToCompareWith"
}
],
"OutputArguments": [
{
"Name": "Result",
"HttpVerb": "POST"
}
]
},
"ActivityId": "Compare"
}
Note: In the script argument - "ISDWGSIMILAR" is a custom command where you will process two drawings, the first one will be current and second one is the drawing to which you are willing compare [ToCompareWith]
Custom NET Command
[CommandMethod("FDACOMMANDS", "ISDWGSIMILAR", CommandFlags.Transparent)]
public static void CompareDrawing()
{
var doc = Application.DocumentManager.MdiActiveDocument;
var db = doc.Database;
var ed = doc.Editor;
var promptResult = ed.GetString("Select Drawing To Compare With");
if (promptResult.Status != PromptStatus.OK) return;
var drawingToCompareWith = promptResult.StringResult;
ed = Application.DocumentManager.MdiActiveDocument.Editor;
using (OpenCloseTransaction o = new OpenCloseTransaction())
{
/*
{
Here your logic code to compare two drawings
}
*/
/*output.txt / json is pushed to your cloud storage as provided in workitem json*/
using (var writer = File.CreateText("output.txt"))
{
if (b != null) /*b value is result of your compare*/
{
writer.WriteLine("TRUE Drawings are same");
}
else writer.WriteLine("FALSE Drawings aren't same");
}
}
}

Parse JSON data with Axios and NodeJS Express (matching a schema for mongoose)

I am trying to parse JSON from an API, and because it has a randomly named property whose value is an object (of which has the data I need), I'm having trouble getting the data from it to match with a schema.
Here is a shortened API response just to show the problem I'm having.
{
"data": {
"1": {
"id": 1,
"name": "First Name",
"quotes": {
"USD": {
"price": 100
}
}
},
"1027": {
"id": 1027,
"name": "Second Name",
"quotes": {
"USD": {
"price": 200
}
}
}
}
}
And a shortened schema:
var coin = new Mongoose.Schema({
id: Number,
name: String,
quotes: {
USD: {
price: Number
}
}
});
So the question is, how would I grab "1"'s object and "1027"'s object without explicitly naming them. And is my schema syntax correct for the objects in question?
Thanks!
You can always use the for...in loop to check whether this data is what you have looking for
const response = {
"data": {
"1": {
"id": 1,
"name": "First Name",
"quotes": {
"USD": {
"price": 100
}
}
},
"1027": {
"id": 1027,
"name": "Second Name",
"quotes": {
"USD": {
"price": 200
}
}
}
}
}
for (let key in response.data) {
if (key === '1') {
console.log('Hey, I find it')
}
console.log(key)
}

JSON transformation in node.js

I want to transform my data from one json structure to another. What is the best way to do it?
Here is my original resource (customer) structure is:
{
"id": "123",
"data": {
"name": "john doe",
"status": "active",
"contacts": [
{
"email": "john#email.com"
},
{
"phone": "12233333"
}
]
}
}
I want to change it to:
{
"id": "123",
"name": "john doe",
"status": "active",
"contacts": [
{
"email": "john#email.com"
},
{
"phone": "12233333"
}
]
}
Keeping in mind that I might have an array of resources(customers) being returned in GET /customers cases. I want to change that to an array of new data type.
If customer object is array of object then below will help you to get desire format result
var result = customerObj.map(x => {
return {
id: x.id,
name: x.data.name,
status: x.data.status,
contacts: x.data.contacts
};
});
here I have used Object.assign() it will be helpful to you
var arr={
"id": "123",
"data": {
"name": "john doe",
"status": "active",
"contacts": [
{
"email": "john#email.com"
},
{
"phone": "12233333"
}
]
}
}
arr=Object.assign(arr,arr.data);
delete arr['data'];
console.log(arr);
You have to Json.parse the json into variable, and then loop through the array of objects, changes the object to the new format, and then JSON.stringify the array back to json.
Example code
function formatter(oldFormat) {
Object.assign(oldFormat, oldFormat.data);
delete oldFormat.data;
}
let parsedData = JSON.parse(Oldjson);
//Take care for array of results or single result
if (parsedData instanceof Array) {
parsedData.map(customer => formtter(customer));
} else {
formatter(parsedData);
}
let newJson = JSON.stringify(parsedData);
console.log(newJson);
Edit
I made the formatter function cleaner by using Kalaiselvan A code

Loading TreeStore with JSON that has different children fields

I am having a JSON data like below.
{
"divisions": [{
"name": "division1",
"id": "div1",
"subdivisions": [{
"name": "Sub1Div1",
"id": "div1sub1",
"schemes": [{
"name": "Scheme1",
"id": "scheme1"
}, {
"name": "Scheme2",
"id": "scheme2"
}]
}, {
"name": "Sub2Div1",
"id": "div1sub2",
"schemes": [{
"name": "Scheme3",
"id": "scheme3"
}]
}
]
}]
}
I want to read this into a TreeStore, but cannot change the subfields ( divisions, subdivisions, schemes ) to be the same (eg, children).
How can achieve I this?
When nested JSON is loaded into a TreeStore, essentially the children nodes are loaded through a recursive calls between TreeStore.fillNode() method and NodeInterface.appendChild().
The actual retrieval of each node's children field is done within TreeStore.onNodeAdded() on this line:
dataRoot = reader.getRoot(data);
The getRoot() of the reader is dynamically created in the reader's buildExtractors() method, which is what you'll need to override in order to deal with varying children fields within nested JSON. Here is how it's done:
Ext.define('MyVariJsonReader', {
extend: 'Ext.data.reader.Json',
alias : 'reader.varijson',
buildExtractors : function()
{
var me = this;
me.callParent(arguments);
me.getRoot = function ( aObj ) {
// Special cases
switch( aObj.name )
{
case 'Bill': return aObj[ 'children' ];
case 'Norman': return aObj[ 'sons' ];
}
// Default root is `people`
return aObj[ 'people' ];
};
}
});
This will be able to interpret such JSON:
{
"people":[
{
"name":"Bill",
"expanded":true,
"children":[
{
"name":"Kate",
"leaf":true
},
{
"name":"John",
"leaf":true
}
]
},
{
"name":"Norman",
"expanded":true,
"sons":[
{
"name":"Mike",
"leaf":true
},
{
"name":"Harry",
"leaf":true
}
]
}
]
}
See this JsFiddle for fully working code.