Update existing field in local json file in React Native - javascript

I'm working on an app where I have some data stored in the local JSON file. Initially, I'm showing that data using SectionList. And there are some fields to update the values of some fields of that data.
My data looks like this:
{
"title": "Terminal Settings",
"data": [
{ "title": "Keypad", "id": 1, "isSelected": false},
{ "title": "Menu", "id": 2 , "isSelected": false},
{ "title": "Tabs", "id": 3, "isSelected": false},
{ "title": "Mobile", "id": 4, "isSelected": false}
]
},
Now from UI, I want to change the status of isSelected field through the checkbox and also want to change it in the JSON file. So whenever I will access this data it should show isSelected: true.
How to do this? Do I need to use AsyncStorage for this?

I think you will definely have to open and modify the content of your file at some point, so your should need some filesystem. The best would be to simple get your json object with
const myData = require('./myDataJsonFile.json');
then modify it with for example $json[0]['data'][1]['isSelected'] = true
and finally save this in your file, replacing the file content with your $json

Related

React. Streaming JSON file

I don't have any backend logic and database. For data I just use a json file of humans.
Here it is:
[
{
"id": 0,
"name": "Andrew"
},
{
"id": 1,
"name": "Daniel"
},
{
"id": 2,
"name": "John"
},
{
"id": 3,
"name": "Frank"
}
]
Can I somehow stream this JSON file in React (i.e. on client side) to, for example, retrieve only the first two notes and return the array? I tried FS module but it only works for SSR. Or there's no way I can runtime stream it getting desired data? I just don't wanna return the whole array since I'm trying to imitate a backend database

Not able to get value from postman response body

I have an API which has response as below:
"message": "Success!",
"user": {
"id": 17,
"first_name_kanji_or_hiragana": "Hiragana",
"last_name_kanji_or_hiragana": "Name",
"first_name_katakana": null,
"last_name_katakana": null,
"email": "user#example.com",
"image_path": null,
"email_verified_at": "2019-03-06 04:44:46",
"type": "admin",
"created_at": "2019-03-06 04:44:46",
"updated_at": "2019-03-06 04:44:46",
"deleted_at": null,
"full_name": "Name Hiragana",
"full_image_path": null,
"roles": [
Each time when i am running the API new user id is generating. What i want to do is that i want to fetch the id each time i run the api & then want to use that id in next response.
I have used below code to fetch the id but it's not working
pm.test(responseBody, true)
var jsonData = JSON.parse(responseBody);
jsonData.data.user[0].id
pm.test(responseBody, true)
var jsonData = JSON.parse(responseBody);
jsonData.data.user[0].id // youre access the user if its array its not
Just use jsonData.data.user.id or jsonData.data.user[id]
You are trying to access an array called user
jsonData.data.user[0].id
But user is an object, so you need to access:
jsonData.data.user.id
There are sometimes that you need to check the complete json directly from your request.
user is not an array. Try the below one.
jsonData.data.user.id
Your solution will work when you have the JSON like below,
"lib_folders": {
"changed": false,
"examined": 5,
"failed": false,
"files": [
{
"atime": 1549608521.3831923,
"ctime": 1548742613.5470872,
"path": "/root/abc/xyz",
"xusr": true
},
{
"atime": 1549608521.4031928,
"ctime": 1548742462.3279672,
"path": "/root/123/456",
"xusr": true
}
],
"matched": 2,
"msg": ""
}
For the above JSON if you want to get the path of the first file, then use the below code,
jsonData.data.lib_folders.files[0].path

How to expand a lookupfield in a sharepoint document library using rest

I have a SharePoint(SP) library with some custom columns.
One of the columns contains a lookup field pointing to a SP list.
I need to retreive the documents in the list and all the custom fields including the fields from the lookup list (join).
There are some Q&A on stackoverflow regarding this topc, but they are about SP lists not SP document libraries.
This is what I have come up with until now (using javascript to perform a GET):
{
"url": "sites/mysite/_api/Web/GetFolderByServerRelativeUrl('/sites/mysite/myfilelibrary')
/files?$select=Name,LinkingUrl,TimeCreated,TimeLastModified,Title,ListItemAllFields
&$expand=ListItemAllFields/myListIdId"
}
This results into:
{
"value": [
{
"ListItemAllFields": {
"FileSystemObjectType": 0,
"Id": 1,
"ServerRedirectedEmbedUrl": "https://mydomain.sharepoint.com/sites/mysite/_layouts/15/WopiFrame.aspx?sourcedoc={abababab-efff-4e69-94de-128ff7c14256}&action=interactivepreview",
"ContentTypeId": "0x010100769480B9F5404447A43367BFC5B86AAB",
"Title": null,
"checkResponsible": null,
>>>>>>>>>>>>"myListIdId": 15, <<<<<<<<<<<<<<<<<<<<<
"SharedWithUsersId": null,
"SharedWithDetails": null,
"ID": 1,
"Created": "2016-07-25T06:04:56",
"AuthorId": 9,
"Modified": "2016-07-26T15:11:27",
"EditorId": 11,
"OData__CopySource": null,
"CheckoutUserId": null,
"OData__UIVersionString": "9.0",
"GUID": "898770aa-da56-40ae-9db1-430bba2da9bd"
},
"LinkingUrl": "https://mydomain.sharepoint.com/sites/mysite/myFileLibrary/Document.docx?d=w3028f42eefff4e6994de128ff7cd6e48",
"Name": "Document.docx",
"TimeCreated": "2016-07-25T13:04:56Z",
"TimeLastModified": "2016-07-26T22:11:27Z",
"Title": ""
}
]
}
The field myListIdId is returned but not expanded.
$expand=ListItemAllFields/myListIdId
should do the trick, but clearly it does not.
What am I missing?
According to this article, you specify the expanded field in the select parameter.
Revised:
"...$select=Name,LinkingUrl,TimeCreated,TimeLastModified,Title,ListItemAllFields/myListIdId
&$expand=ListItemAllFields"

Filter store by Id has coincidences with other store in Sencha Touch

I am working in a Sencha touch app, and I am finding problems to filter correctly the next scenario:
I have this API:
In this area we have different templates number related with the Id, data is saved in a store call (for example: templates):
{
"id": "0100100001",
"templates": {
"id": "0000000187", ---> customerId
"name": "TEST01",
"validFrom": "\/Date(1360710000000)\/",
"validTo": "\/Date(253402300799000)\/"
},
{
"id": "0000000188",
"name": "S_TEST_01",
"validFrom": "\/Date(1360710000000)\/",
"validTo": "\/Date(253402300799000)\/"
}
}
In other Json/store called "Survey" we save the next data,
"id": "0000104747",
"templateId": "0000000955",
"templateName": "SURVEYAPP TEST1",
"customerId": "0100100001",
Question is: How can I evaluate if the customerId has a template created in the "Surveys" store?
Thank you in advance.

Retaining the state of an object in JavaScript & convert it into text which can be passed as parameter(as object) to another function

We have a UI where user makes selection & click on process button. On click on button, system calls a method written in JavaScript and it returns a object which looks like this.
{
"users": [{
"id": 1,
"name": "Ed",
"orders": [{
"id": 10,
"total": 10.76,
"status": "invoiced"
},{
"id": 11,
"total": 13.45,
"status": "shipped"
}]
}]
}
What I want:
I want to pass this JavaScript object to a method which should generate a text producing output like this:
{
"users": [{
"id": 1,
"name": "Ed",
"orders": [{
"id": 10,
"total": 10.76,
"status": "invoiced"
},{
"id": 11,
"total": 13.45,
"status": "shipped"
}]
}]
}
I should be able to pass a real JavaScript object to this method and by going over the object it should produce a text showcasing the structure of this object.
In .net world we can do this by using reflection and then return the string. We also have option of serializing the object into XML or JSON or any other format.
Is it possible with JavaScript.
Why I want to do this.
I have written 50 test cases which expects this object as input. I can take output of the method and pass it to any testcase.
Thank you
You should add your stringified object to some <pre> and <code> tags to get the best output.
<div><pre><code class="text"></code></pre></div>
And then use the JSON.stringify spaces parameter:
$('.text').html(JSON.stringify(obj, null, 2));
You can also use tabs if you want.
$('.text').html(JSON.stringify(obj, null, '\t'));
Fiddle
Use JSON.stringify() method. It does exactly what you need.

Categories