Fixing local JSON file for working with structs - json

I have a local json file like this
{
"scroll": [
{
"id": 0,
"titles": "1",
"course": ["first", "Second"]
},
{
"id": 1,
"titles": "2",
"course": ["Third", "Fourth"]
}
]
}
So, I want that every item of "course" has its own string parameter. For example, "Third" has "third" parameter, "Fourth" has "fourth" parameter and so on. But I don't know how to do it in json file. I want that clicking "first" button in the list it navigate to the text that is written in json, and after clicking in "Second " button it navigate to another text. Please help me to solve this problem. Thanks.

Related

How to write JSON Path to get particular value based on string filter?

I am new to JSON Path and I am trying to get 'id' corresponding to name='Candy' using JsonPath in below JSON payload.
{
"responsePayload": [
{
"content": {
"id": "101",
"name": "Candy"
},
"links": [
{
"rel": "self",
"link": "api/v1/sweets/101",
"id": "101"
}
]
},
{
"content": {
"id": "102",
"name": "Chocolate"
},
"links": [
{
"rel": "self",
"link": "api/v1/sweets/102",
"id": "102"
}
]
}
]
}
For this I tried Jsonpath $.responsePayload[0].content[?(#.name=='Candy')].id but that is not working. I am using https://jsonpath.com/ for testing this JsonPath. Please guide me how I can achieve required result.
You're really close. You need more of the path inside the expression.
$.responsePayload[?(#.content.name=='Candy')].content.id
The [0] you have in your response isolates the first element only, but it sounds like you want to iterate over the whole array in responsePayload. To do that, the filter expression selector [?(...)] must act on that array.
Next, the # represents the current item, and you can build a full path off of it. You can see how that works above.
Finally, the filter expression selector returns the full item when the expression returns true. So you then need to navigate into .content.id again to get the value you're after.

How to add new object in a json file using RobotFrameWork

I am trying to add a new bloc to my JSON
I have this JSON that I got after a GET :
{
"name": "John",
"age": 30,
"Deleted": false
}
What I want to do is to add a block trips to this Json using RobotFrameWOrk to get this result:
{
"name": "John",
"age": 30,
"trips": [
{
"dateTime": "2020-01-24T15:28:29.7073727Z",
"FirstName": "John",
"Comment": "TestMe"
}
],
"Deleted": false
}
My questions are:
The object Trips doesn't exist I have to create it manually
and then I should add this object to my JSON after the age and before Deleted
${JsonFinall}= Add String ${FirstJson} ${BlockTrips}
Log ${JsonFinall}
I imagine it would be something like that but I am blocked on the first step I don't know how to create and fill the object trips?
Do you think that I have to work with Regex?
Thanks for your help
***********************EDIT**********
I tried with add object to json : `# Bloc ActionR
${jsonFinall}= Add Object To Json ${JsonAvecAR} Course/AR.txt`
the file AR.txt is the file where I put my object trips :
"trips": [
{
"dateTime": "2020-01-24T15:28:29.7073727Z",
"FirstName": "Alicia",
"Comment": "TestMe"
}
],[![enter image description here][1]][1]

Parsing through JSON .. Gives undefined?

I have a very complex JSON and a snippet of it is below:
var designerJSON=
{
"nodes":
[
{
"NodeDefinition": {
"name": "Start",
"thumbnail": "Start.png",
"icon": "Start.png",
"info": "Entry point ",
"help": "Start point in your workflow.",
"workflow ": "Start",
"category": "Basic",
"ui": [
{
"label": "Entry point",
"category": "Help",
"componet": "label",
"type": "label"
}
]
},
"States": [
{
"start": "node1"
}
]
},.......
]
}
I would like to get the value of "start" in States. But I am stuck in the first step of entering into JSON. When I try
console.log(designerJSON["nodes"]);
I am getting Undefined.
I want the value of start. Wich is designerJSON["nodes"]["States"]["start"].
Can you help.
Thanks in advance
designerJSON["nodes"]["States"]["start"] won't do it.
designerJSON["nodes"] is a list, as is States, so you need to access individual items by index (or iteration).
In the example you have given you need to use this:
designerJSON['nodes'][0]['States'][0]['start']
or this (cleaner IMO):
designerJSON.nodes[0].States[0].start
You have an array in JSON.
instead of
designerJSON["nodes"]["States"]["start"]
use
designerJSON["nodes"][0]["States"][0]["start"]
ps. pay attention on how code is formatted in the topic.
pps. using brackets for accessing properties in js is "bad style" (due to js hint recommendations). better access those via dot, e.g:
designerJSON.nodes[0].States[0].start

How to load JSON to D3 in Tree Diagram

I am a real dumb with HTML and JavaScript, so please excuse any dumbness.
I am using D3 Tree Diagram, but I need to load a JSON file instead of writing it inside the JS script, which the name of the file to be loaded will be chose by the user in a select tag. Here's the D3 code
First, how can I load/read a JSON file, lets say exampleNodes.json,
And then, how can I pass the name of the selected select tag so that it reads the appropriate JSON?
Thanks for your patience, and help. Thank you.
in code
var treeData = [
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
}
];
you have to save it on data.json file like
{
"treeData" : [ ... your data array ...]
}
after that in d3.json() function you will receive this object
d3.json("data.json",function(json){
// do your coding
// or all code put inside one function and call it after data loaded
});
if you are using google chrome than it will gave you error on data reading from json because security Google Chrome not allow read files from file system you can get data in Firefox. to make it run upload your code on some local server. i.e in WampServer or Apache tomcat etc.

Obtain a different JSON object structure in AngularJS

I'm Working on AngularJS.
In this part of the project my goal is to obtain a JSON structure after filling a form with some particulars values.
Here's the fiddle of my simple form: Fiddle
With the form I will do a query to KairosDB, that is my NoSql Database, I will query data from it by a JSON object. The form is structured in this way:
a Name
a certain Number of Tags, with Tag Id ("ch" for example) and tag value ("932" for example)
a certain Number of Aggregators to manipulate data coming from DB
Start Timestamp and End Timestamp (now they are static and only included in the final JSON Object)
After filling this form, with my code I'll obtain for example this JSON object:
{
"metrics": [
{
"tags": [
{
"id": "ch",
"value": "932"
},
{
"id": "ch",
"value": "931"
}
],
"aggregators": {
"name": "sum",
"sampling": [
{
"value": "1",
"unit": "milliseconds",
"type": "SUM"
}
]
}
}
],
"cache_time": 0,
"start_absolute": 123,
"end_absolute": 1234
}
Unfortunately, KairosDB accepts a different structure, and as you could see, Tag id "ch" doesn't hase an "id" string before, or for example, Tag values coming from the same tag id are grouped together
{
"metrics": [
{
"tags": {
"ch": [
"932",
"931"
]
},
"name": "AIENR",
"aggregators": [
{
"name": "sum",
"sampling": {
"value": "1",
"unit": "milliseconds"
}
}
]
}
],
"cache_time": 0,
"start_absolute": 1367359200000,
"end_absolute": 1386025200000
}
My question is: Is there a way to obtain the JSON structure like the one accepted by Kairos DB with an Angular JS form?. Thanks to everyone.
I've seen this topic as the one more similar to mine but it isn't in AngularJS.
Personally, I'd do the refactoring work in the backend - Have what ever server interfaces sends and receives data do the manipulation - Otherwise you'll end up needing to refactor your data inside Angular anywhere you want to use that dataset.
Where as doing it in the backend would put it in a single access point.
Of course, you could do it in Angular, just replace userString in the submitData method with a copy of the array and replace the tags section with data in the new format, and likewise refactor the returned result to the correct format when you get a reply.