How to move JSON fields in mass effectively? - json

I have a JSON like this:
{
"array1": [
{
"data": {
"id": "1",
"name": "foo",
},
"classes": "class1"
},
{
"data": {
"id": "2",
"name": "bar",
},
"classes": "class2"
...
}
}
I want to move all the classes into data object. Using https://jsoneditoronline.org I can only do that for each object individually. I guess I can write a script to do this, but is there a built tool for this?

I don't think there will be any tool for such custom requirement. May be you can try the following script.
json.array1.forEach(arr => {
arr.data['classes'] = arr.classes;
delete arr.classes;
})

It seems that regex is the quickest way to do this. Here is how I do:
Search: ("data".*?)(}.*?)(,"classes":.*?)}
Replace: $1$3$2}

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.

JsonPath filter query syntax

I'm trying to write a JsonPath query that selects a specific object based on a condition, but either the syntax fails me or I fail the syntax.
Given the below Json object, how would I select the "Data" object containing Dirk Gently's details based on the fact that he uses the "Stumble" method?
{
"Investigators": [
{
"Type": "Legend",
"Object": {
"Method": "Investigate",
"Data": {
"Name": "Sherlock",
"Surname": "Holmes"
}
}
},
{
"Type": "Visionary",
"Object": {
"Method": "Stumble",
"Data": {
"Name": "Dirk",
"Surname": "Gently"
}
}
}
],
"Version": 1
}
I know that I can get to the Method-field like this:
$.Investigators..Object.Method
I assumed that something like this would work:
$.Investigators..Object[?(#.Method=="Stumble")].Data
I'm testing this using: https://jsonpath.com/ to evaluate the query - and I can't seem to get it right.
Am I trying to do something that is not achievable or am I just making a stupid mistake?
Go to this jsonpath online sandbox and try this experssion:
$.Investigators..Object[?(#.Method=="Stumble")].Data
using either the Jayway or Gatling implementations, the output will be:
[
{
"Name" : "Dirk",
"Surname" : "Gently"
}
]

Collect JSON objects with same attribute value, and create new key/value pairs

Here is a simplified sample of the JSON data I'm working with:
[
{ "certname": "one.example.com",
"name": "fact1",
"value": "value1"
},
{ "certname": "one.example.com",
"name": "fact2",
"value": 42
},
{ "certname": "two.example.com",
"name": "fact1",
"value": "value3"
},
{ "certname": "two.example.com",
"name": "fact2",
"value": 10000
},
{ "certname": "two.example.com",
"name": "fact3",
"value": { "anotherkey": "anothervalue" }
}
]
The result I want to achieve, using jq preferably, is the following:
[
{
"certname": "one.example.com",
"fact1": "value1",
"fact2": 42
},
{
"certname": "two.example.com",
"fact1": "value3",
"fact2": 10000,
"fact3": { "anotherkey": "anothervalue" }
}
]
Its worth pointing out that not all elements have the same name/value pairs, by any means. Also, values are often complex objects in their own right.
If I was doing this in Python, it wouldn't be a big deal (and yes, I can hear the chorus of "do it in Python" ringing in my ears now). I would like to understand how to do this in jq, and it's escaping me at the moment.
... using jq preferably ...
That's the spirit! And in that spirit, here's a concise solution:
map( {certname, (.name): .value} )
| group_by(.certname)
| map(add)
Of course there are other reasonable solutions. If the above is at first puzzling, you might like to add a debug statement here or there, or you might like to explore the pipeline by executing the first line by itself, etc.

JSON is it best practice to give each element in an array an id attribute?

Is it best practice in JSON to give objects in an array an id similar to below?. Im trying to decide on a JSON format for a restful service im implementing and decide include it or not... If it is to be modified by CRUD operations is it a good idea?
{
"tables": [
{
"id": 1,
"tablename": "Table1",
"columns": [
{
"name": "Col1",
"data": "-5767703747778052096"
},
{
"name": "Col2",
"data": "-5803732544797016064"
}
]
},
{
"id": 2,
"tablename": "Table2",
"columns": [
{
"name": "Col1",
"data": "-333333"
},
{
"name": "Col2",
"data": "-44444"
}
]
}
]
}
Client-Generated IDs
A server MAY accept a client-generated ID along with a request to
create a resource. An ID MUST be specified with an "id" key, the value
of which MUST be a universally unique identifier. The client SHOULD
use a properly generated and formatted UUID as described in RFC 4122
[RFC4122].
jsonapi.org

Using Oracle UCM JSON data in fullcalendar?

Is there a way to fetch data from Oracle UCM's JSON option and use this in Full Calendar. I createed a service to return data from calendar data in UCM, and want to display the events using fullcalendar.
Here is an example feed that I get back:
"ResultSets": {
{
"SQLLMCal": {
"fields": [
{ "name": "SINGLE_ELEMENT" },
{ "name": "Start" },
{ "name": "SCHOOL_TYPE" },
{ "name": "SCHOOL_TYPE_ID" },
{ "name": "Title" },
{ "name": "ENTRY_SIDE_GROUP" }
],
"rows": [
[
"141",
"3/17/11 12:00 AM",
"Elementary",
"3",
"Big Burger",
"1"
]
]
}
}
}
Thanks, Ken
need to parse your own data using a custom events function:
http://arshaw.com/fullcalendar/docs/event_data/events_function/
Okay, great example of "problem exists between keyboard and chair"! I just had to stare at the documentation and the answer was right there.
In my code, I have the following:
events: function(callback) {
By definition, this MUST be
events: function(start, end, callback) {
Once I added the start and end parameters, everything worked perfectly.
Thank you Adam for pointing me the the right direction!
Ken