Modify the data within the JSON array using Github Actions

I currently have a JSON file with the following structure stored in the directory where my Github action is running -

{
   "version":"1",
   "sampleArray":[
      {
         "id":"1"
      }
   ],
   "secondArray":[
      {
         "secondId":"2"
      }
   ]
}

Is there a way to use Github actions to modify the value of id, for example, changing the id to "5" inside the sampleArray so that the JSON has an updated value?

Answer №1

If you wish to modify a JSON file directly from the command line, you can utilize the powerful tool called jq. Here's an example of how you can achieve this:

on: [push, pull_request]
name: Build
jobs:
  build:
    name: Example
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: Update config.json
        run: echo "`jq '.sampleArray[0].id="5"' config.json`" > config.json
      - name: read config.json
        run: cat config.json

Another method is to combine jq with sponge from the moreutils package and set an environment variable as shown below:

on: [push, pull_request]
name: Build
jobs:
  build:
    name: Example
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - name: install more-utils
        run: sudo apt-get install moreutils
      - name: Update config.json
        env:
          ID: 5
        run: jq --arg id "$ID" '.sampleArray[0].id=$id' config.json | sponge config.json
      - name: read config.json
        run: cat config.json

The resulting output would be:

{
    "version": "1",
    "sampleArray": [{
        "id": "5"
    }],
    "secondArray": [{
        "secondId": "2"
    }]
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Incorporate React into current Node express application by utilizing a content delivery network (CD

I'm currently in the process of developing a web application utilizing both Node and React. Instead of having separate Node and React applications, I decided to integrate React directly into my existing setup. To achieve this, I attempted importing th ...

Arrange the strings in the json data based on their frequency in descending order while maintaining the original sequence of keys in

I am working with a JSON file that contains orders in arrays under the same key. [ { "order":["Order1"] }, { "order":["Order2"] }, { "order":["Order2","Order3"] }, { "order":["Order1","Order2"] }, { ...

Checkbox enabled Bootstrap image

I am encountering a minor issue where I am attempting to incorporate a simple image that can be toggled on and off by clicking on it. After coming across some bootstrap code online, I decided to test it in my project. Unfortunately, the code does not seem ...

When a JavaScript variable refuses to cooperate and won't populate

I am currently working with a simple variable that is accepting a JSON Object. Here is the code snippet: To maintain privacy, I have sanitized the code to avoid revealing sensitive information. var server = "00.00.000.00:8800"; var webServices = "http:/ ...

The dispatch function in redux-thunk is not functioning as expected

Having trouble with thunk and async dispatching? Check out this code snippet: function fetchProvider() { return (dispatch) => { graphqlService(fetchProviderQuery) .then((result) => { dispatch({ type: FETCH_PROVIDER, ...

ReactJS allows for the use of immutable values while still being able to

In my current project, I have predefined values for yearOfEnforcementProceesdings + cityOfEnforcementProceedings. Additionally, there is a serialNumber input field that requires user input. The goal is to display two more unaltered values whenever the seri ...

Utilize jQuery to dynamically add or remove elements by referencing specific HTML elements

My goal is to dynamically add an element to a dropdown menu and then remove it after selection. I initially attempted to define the htmlElement reference in this way: (Unfortunately, this approach did not work as expected) var selectAnOption = "<option ...

Encountering difficulties in sending the data to Firebase through the contact form

import React, { useState } from 'react' import styled from 'styled-components' import Title from '../Components/Title' import { InnerLayout, MainLayout } from '../Styles/Layout' import ...

Decoding JSON Data in Angular 2

I am facing a challenge where I have an array of JSON objects that need to be parsed into my Tournament class. The Tournament class has the following structure: export class Tournament { constructor (public id: number, public name: string, ...

"Troubleshooting issue with setAttribute not functioning properly when used with onClick in

A new variable named "navBtn" is created by utilizing document.createElement("span") to generate a span element. However, for unknown reasons, applying setAttribute to this span is not functioning as expected. The navBtn variable resides within its own fun ...

The desired jQuery datatable theme did not take effect on the JSP page

Experimenting with various JQuery themes and controls. Attempting to use a datatable example and apply the default theme provided here. However, I have been encountering difficulties. Seeking assistance to understand the root cause of this issue. Also, in ...

Error encountered while parsing JSON data for a particular string

Seeking a solution: Presenting my data: {Path:5984fcb4-8bf8-4205-86f8-e6e2042ba610.jpg,StatusCode:OK} Unable to convert the string to Json using the method below: var obj = $.parseJSON(data); This website also states that my json is not formatted corr ...

Guide to setting up collapsible sections within a parent collapsible

I came across this animated collapsible code that I'm using: https://www.w3schools.com/howto/howto_js_collapsible.asp Here is the HTML: <button type="button" class="collapsible">Open Collapsible</button> <div class="content"> &l ...

Working with npm objects across multiple files

My goal is to integrate the npm package for parallax (lax.js) into my project. The documentation states that in order to initialize it, the following code snippet should be included: window.onload = function () { lax.init() // Add a driver that we use ...

I need to find a way to properly test a recursive, async JavaScript function by utilizing fake timers

I've been working with a basic recursive function that performs as expected when run. To thoroughly test its operation at each stage, I want to utilize Sinon's fake timers. Unfortunately, it seems that the fake timers are only affecting the init ...

Ways to expand the `Array.prototype` from an external library in a Node.js environment

While enjoying my time on hackerrank with pure JavaScript, I decided to steer clear of extra arrays or math libraries, unlike the convenience of using python. My approach is robust, but now I'm considering utilizing sugar.js or underscore. I came acr ...

Using JQuery to parse data in a $.post request

My goal is to dynamically populate textboxes using JQuery based on an array sent from an external PHP file through json_encode(array). The chosen option in a select box determines which value is sent to the PHP file. Despite my efforts to debug using tools ...

Endless Loop Encountered When Attempting to Split a Vuex Array

If you want to check out my codesandbox setup, you can find it here. In this setup, three dates should be printed. The important parts of the code are as follows: import Vue from "vue"; import App from "./App"; import Vuex from "vuex"; Vue.use(Vuex); co ...

Modify the form's action attribute when submitted

I am trying to create a form with multiple buttons that will change the action and target properties when a specific button is clicked. Below is my code snippet: <div id="root"> <form :action="form.action" ref="form" :target="form.target"&g ...

What is the best approach to managing a Symfony form that contains TWO CollectionType child forms?

I have been referring to the Symfony guide here which explains how to generate and save a collection of associated entities within a form. Following the example provided in the guide, I have successfully implemented the functionality for adding and removi ...