Json data Copy and replace automate - json

Any suggestion would be appreciated.
I have a json file with close to 10000 elements, out of which I need to make a copy of 2000 elements based on a condition and replace an attribute when doing so..
Ex.
Input : jsonData.json
//Other elements
{
"_id" :ObjectId("123"),
"_arryAtt" : {
"attr1":"dummy",
"attr2":"123"
}
}
//Other elements
Output : jsonData.json
//Other elements
{
"_id" :ObjectId("123"),
"_arryAtt" : {
"attr1":"dummy",
"attr2":"123"
}
}
{
"_arryAtt" : {
"attr1":"dummy",
"attr2":"567"
}
}
//Other elements
Id is missing in the copied object..
I am familiar with java
How can I achieve this if I don't' want to write a custom parser...
What kind of scripting can I use to automate this, as running this manually to copy 2000 elements and change one attribute is difficult..

Related

Searching for an item by a known value of an element in the array of an element in the array

I have a known value for an element, ENTRYID in my JSON Document. I want to get the Tasks.Id for the element of Tasks that has an ENTRYID matching my known value. The issue is that ENTRYID is a little bit buried inside Tasks. As an example, here is the JSON Path to find the first ENTRYID in the document.
$.Tasks[0].Activities[0].Entry.AnswersFlat[0].ENTRYID
For pathing to ENTRYID from a Tasks element, it is always in Activities[0].Entry.AnswersFlat[0]. Those two arrays do not need to be searched.
I know I can use ?(#.Tag == 'Known Value') to find elements that contain a known value for a specific tag name but when I try to use that in my JSON path, I keep getting errors. I've tried:
$.Tasks[?(#.Activities[0].Entry.AnswersFlat[0].ENTRYID=='95B53C6CF6104F0E86B5744083A22FAD')].Id
But this returns an error.
I have also tried nesting the filters but that also returns an error :
$.Tasks[?(#.Activites[?(#.Entry.AnswersFlat[?(#.ENTRYID=='95B53C6CF6104F0E86B5744083A22FAD')])])].Id
What is the correct syntax in JSON Path to get the Tasks.Id for my known value of ENTRYID?
I have created a very simplified version of my document for your reference:
{
"Tasks":[
{
"Id":"652a187f2a584a0f84aaadb9016cfa4b",
"Activities":[
{
"Entry":{
"AnswersFlat":[
{
"ENTRYID":"95B53C6CF6104F0E86B5744083A22FAD"
}
]
}
}
]
},
{
"Id":"e586073810a746cca440adb901090326",
"Activities":[
{
"Entry":{
"AnswersFlat":[
{
"ENTRYID":"7FD608F3F9E24EB084F58A1245677B3D"
}
]
}
}
]
}
]
}

How can I see the Path or Ancestors in a large json file

I am working with a very large json file, in Atom, I want to know if there is a way to see the json path of where my cursor is at but can't find a package to help me
For example, if my cursor is at Y below, I want something which says
grandparent2:parent:child
I do not wish to collapse all the json to see this.
If Atom cannot help is there another editor which can?
{
"grandparent" : {
"parent" : {
"child" : {
"property" : "X"
}
}
}
"grandparent2" : {
"parent" : {
"child" : {
"property" : "Y"
}
}
}
}
You can refer json editor(https://jsoneditoronline.org/). See the example below and click on node and you will see the path:
https://jsoneditoronline.org/#left=cloud.e461c68e9a9c491781169eeac7b651da&right=local.qefaya
It will look like

Gatsby: combining two graphql sources on a page (.json and .jpg sources)

context
I am turning an old php ecommerce site into a static site built with gatsby.js.
I put all my product metadata into separate .json files (one per product) and I am able to load them with json and file transformer plugins. They are in /items. However each item is related to a set of standard images... eg item-01-main.jpg, item-01-placement.jpg, etc... I put all the images together in /itemphotosand followed the instructions here to load them into graphql: https://www.gatsbyjs.org/packages/gatsby-image/
question
On the page that loads all the products, i have no idea how to incorporate each one's item-xx-main.jpg image: I don't know what graphql query would fetch BOTH sets of data and match/merge them.:
/items
item-01.json
item-02.json
/itemphotos
item-01-main.jpg
item-01-placement.jpg
item-02-main.jpg
item-02-placement.jpg
I have a feeling my directory structure was the wrong approach and maybe I should store all the related images with my product json together in a folder:
/items
item-01/
item-01.json
item-01-main.jpg
item-01-placement.jpg
item-02/
item-02.json
item-02-main.jpg
item-02-placement.jpg
But then how do I source an items/ directory made up of item-xx/ subfolders holding both images and json, as representing a single entity, in graphql?
I did not go with markdown files because I wanted max UI flexibility.
Structure 2
Can you reference your images inside the .json? If so, the 2nd structure could be the winner, since you won't have to do much extra work:
// item-01.json
{
"meta": "...",
"main": "./item-01-main.jpg",
"placement": "./item-01-placement.jpg"
}
Query:
{
json {
id
main {
childImageSharp {
fixed {
src
}
}
}
}
}
Structure 1
If that's not possible or if you'd like to keep the 1st structure, you can try this query:
{
item: allFile(filter: { relativePath: { regex: "/item-01/" } }) {
nodes {
name
extension
children {
... on ImageSharp {
fixed {
src
}
}
... on Item01Json {
id
}
}
}
}
}
This will yield an array containing all files that share the same item id, regardless where they are stored. You can then use extension field to find the json & jpg nodes. It's not pretty, but it also doesn't require that much additional work.
None of the above
If none of that works for you, you could explore adding a image field to the json's graphql schema with createTypes and createResolvers. Add a type definition for the json via createTypes, then use createResolvers to locate the imageSharp node & resolve it.
A more clear cut example can be found from their code repo this page. This uses the same method as #Derek discussed. But if you want an example to understand more.

JSON Deserialization on Talend

Trying to figuring out how to deserialize this kind of json in talend components :
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
"ryan.buckley#toofr.com": {
"confidence":18,"email":"ryan.buckley#toofr.com","default":16
},
"ryanbuckley#toofr.com": {
"confidence":17,"email":"ryanbuckley#toofr.com","default":17
},
"ryan_buckley#toofr.com": {
"confidence":16,"email":"ryan_buckley#toofr.com","default":18
},
"ryan-buckley#toofr.com": {
"confidence":15,"email":"ryan-buckley#toofr.com","default":19
},
"ryanb#toofr.com": {
"confidence":14,"email":"ryanb#toofr.com","default":14
},
"buckley#toofr.com": {
"confidence":13,"email":"buckley#toofr.com","default":13
}
}
This JSON comes from the Toofr API where documentation can be found here .
Here the actual sitation :
For each line retreived in the database, I call the API and I got this (the first name, the last name and the company change everytime.
Does anyone know how to modify the tExtractJSONField (or use smthing else) to show the results in tLogRow (for each line in the database) ?
Thank you in advance !
EDIT 1:
Here's my tExtractJSONfields :
When using tExtractJSONFields with XPath, you need
1) a valid XPath loop point
2) valid XPath mapping to your structure relative to the loop path
Also, when using XPath with Talend, every value needs a key. The key cannot change if you want to loop over it. Meaning this is invalid:
{
"ryan#toofr.com": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"rbuckley#toofr.com": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
but this structure would be valid:
{
"contact": {
"confidence":119,"email":"ryan#toofr.com","default":20
},
"contact": {
"confidence":20,"email":"rbuckley#toofr.com","default":15
},
So with the correct data the loop point might be /contact.
Then the mapping for Confidence would be confidence (the name from the JSON), the mapping for Email would be email and vice versa for default.
EDIT
JSONPath has a few disadvantages, one of them being you cannot go higher up in the hierarchy. You can try finding out the correct query with jsonpath.com
The loop expression could be $.*. I am not sure if that will satisfy your need, though - it has been a while since I've been using JSONPath in Talend because of the downsides.
I have been ingesting some complex json structures and did this via minimal json libraries, and tjava components within talend.

Create dynamic controls from JSON Response SAP UI5

I have a requirement to create a group of checkbox with lebels in a page from JSON response.
The JSON response is
{
"data": [
{
"CodeStatus": "Red"
},
{
"CodeStatus": "Orange"
},
{
"CodeStatus": "Green"
},
{
"CodeStatus": "Yellow"
}
]
}
the design is:
the box is vertically scrollable, as it can have more color codes.
Can anyone suggest how to design the above?
If one can provide the implemantation code, that would be very good as I have very time to implement it.
Create a JsonModel . use the setData() method to define its data from the json you got on the response.
Then use a VerticalLayout element and bind its content to "/data" on the model.
This will create a "line" for each element on the data attribute of your model.
Use a template for the line that is a label+checkbox where the label text is bound to "CodeStatus".
You also need a checked attribute on each entry to store the checked attribute of the checkbox.