Coder Social home page Coder Social logo

Comments (2)

dakusui avatar dakusui commented on August 23, 2024

This appears to be an issue which is observed for merging incompatible nodes.
That is, given, a.json and b.json, each of which is a JSON object.
And at the same path .node, b.json has an array and the other has an atom (string, number, ...).

from jq-front.

dakusui avatar dakusui commented on August 23, 2024
#!/bin/env bash                                                                                                                                                                                             
set -eu +o posix -o pipefail

function debug() {
  echo "${@}" >&2
}

function abort() {
  debug "${@}"
  exit 1
}

# Latter overrides former                                                                                                                                                                                   
function _merge_object_nodes() {

  local _a="${1}"
  local _b="${2}"
  local _error
  _error=$(mktemp)
  debug "merging _a:'${_a}' and _b:'${_b}'"
  # shellcheck disable=SC2016                                                                                                                                                                               
  jq -r -c -n --argjson a "${_a}" --argjson b "${_b}" '#----                                                                                                                                                
def value_at($n; $p):                                                                                                                                                                                       
  $n | getpath($p);                                                                                                                                                                                         
                                                                                                                                                                                                            
def setvalue_at($n; $p; $v):                                                                                                                                                                                
  $n | setpath($p; $v);                                                                                                                                                                                     
                                                                                                                                                                                                            
def type_of($v):                                                                                                                                                                                            
  $v | type;                                                                                                                                                                                                
                                                                                                                                                                                                            
def setvalue_at($n; $p; $v):                                                                                                                                                                                
  $n | if type_of($v)=="object" or type_of($v)=="array" then                                                                                                                                                
         if type_of(value_at($n; $p))!="object" and type_of(value_at($n; $p)!="array") then                                                                                                                 
           setvalue_at(.; $p; $v)                                                                                                                                                                           
         else                                                                                                                                                                                               
           .                                                                                                                                                                                                
         end                                                                                                                                                                                                
       else                                                                                                                                                                                                 
         setvalue_at(.; $p; $v)                                                                                                                                                                             
       end;                                                                                                                                                                                                 
                                                                                                                                                                                                            
def merge_objects($a; $b):                                                                                                                                                                                  
  $b | [paths(scalars_or_empty)]                                                                                                                                                                            
     | reduce .[] as $p ($a; setvalue_at(.; $p; value_at($b; $p)));                                                                                                                                         
                                                                                                                                                                                                            
merge_objects($a; $b)' || {
    abort "Failed to merge object nodes:'${_a}' and '${_b}': error: ${_error}"
  }
}

_merge_object_nodes '{"key":"value"}' '{"key":["elem1"]}'

from jq-front.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.